【发布时间】:2014-05-29 05:05:06
【问题描述】:
我正在开发一个 Android 应用程序,我必须在其中解析 Json 数组并将其调整为自动完成文本视图。我从我的服务器得到了正确的响应,因为我对 android 和 Json 解析比较陌生,所以我不知道如何将该 json 数组解析为数组适配器并将其添加到自动完成文本视图中。
任何机构都可以建议我怎么做吗..?
回复如下。
[
{
city: "bng",
area: "anagar",
id: 510000032
},
{
city: "bng",
area: "bnagar",
id: 510000021
},
{
city: "bng",
area: "cnagar",
id: 510000037
}
]
我希望在我的自动完成文本视图中匹配区域。
我在布局文件中创建了自动完成文本视图。
这就是我的尝试
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;
}
}
欢迎提出任何建议...
【问题讨论】:
标签: android arrays json autocomplete autocompletetextview