rub*_*bin 4 arrays android json autocomplete autocompletetextview
我正在开发一个Android应用程序,我必须解析一个Json数组并使其适应自动完整的文本视图.我从我的服务器得到正确的响应,因为我是一个更新的android和Json解析我没有得到如何解析该json数组到数组适配器并将其添加到自动完整文本视图.
任何人都可以建议我怎么做..?
回复如下.
[
{
city: "bng",
area: "anagar",
id: 510000032
},
{
city: "bng",
area: "bnagar",
id: 510000021
},
{
city: "bng",
area: "cnagar",
id: 510000037
}
]
Run Code Online (Sandbox Code Playgroud)
我想在我的自动完整文本视图中匹配区域.
我在布局文件中创建了自动完整文本视图.
这就是我的尝试方式
public class MainActivity extends Activity {
List<String> responseList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView auto1 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
new HttpGetTask().execute();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_2, responseList);
auto1.setAdapter(adapter);
}
private class HttpGetTask extends AsyncTask<Void, Void, String> {
String URL = "http://xyz/cities.json?app_id=test";
AndroidHttpClient mClient = AndroidHttpClient.newInstance("");
@Override
protected String doInBackground(Void... params) {
HttpGet request = new HttpGet(URL);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
return mClient.execute(request, responseHandler);
} catch (ClientProtocolException exception) {
exception.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
try {
JSONArray json = new JSONArray(result);
Log.v("ResponseCity", result);
responseList = new ArrayList<String>();
for (int i = 0; i < json.length(); i++) {
final JSONObject e = json.getJSONObject(i);
String name = e.getString("area");
responseList.add(name);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (null != mClient)
mClient.close();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
任何建议都表示赞赏......
添加响应字符串成List<String>这样
List<String> responseList = new ArrayList<String>();
for (int i = 0; i < json.length(); i++) {
final JSONObject e = json.getJSONObject(i);
String name = e.getString("area");
responseList.add(name);
}
Run Code Online (Sandbox Code Playgroud)
现在,你可以设置响应列表AutoCompleteTextView这样
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, responseList);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.autoCompleteTextView1);
textView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16705 次 |
| 最近记录: |