我的 android 应用程序中有一个列表视图,它显示来自下载和解析 json 的数据。在这个列表视图中,OnItemClickListener 在前五个或六个项目后没有响应。谁能告诉我我的问题是什么?
这是我的 listview 代码:
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
try {
String s = BookJsonParser.ids[arg2];
String bookDetailUrl = url + s;
DownloadBookDetail downloadTaskbookDetail = new DownloadBookDetail();
downloadTaskbookDetail.execute(bookDetailUrl);
} catch (Exception e) {
System.out.println(e.printStackTrace(););
}
}
});
DownloadBookDetail 是一个 asyncTask,其中 Json String 在它的 doInBackGround 方法中下载,并且它在它的 onPostExecute 方法中打开另一个 asyncTask。在第二个 asyncTask 中,我在 doInBackground 方法中解析 json 并在 onPostExecute 方法中使用适配器加载列表视图。第二个 asyncTask 的代码:
/** AsyncTask to parse json data and load ListView */
private class ListViewLoaderTask extends
AsyncTask<String, Void, SimpleAdapter> {
JSONObject jObject;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(BookActivity.this);
pDialog.setMessage("Listing New Books...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
// Doing the parsing of xml data in a non-ui thread
@Override
protected SimpleAdapter doInBackground(String... strJson) {
try {
jObject = new JSONObject(strJson[0]);
BookJsonParser countryJsonParser = new BookJsonParser();
countryJsonParser.parse(jObject);
} catch (Exception e) {
Log.d("JSON Exception1", e.toString());
}
// Instantiating json parser class
BookJsonParser countryJsonParser = new BookJsonParser();
// A list object to store the parsed countries list
List<HashMap<String, Object>> countries = null;
try {
// Getting the parsed data as a List construct
countries = countryJsonParser.parse(jObject);
System.out.println(countries.toString());
} catch (Exception e) {
Log.d("Exception", e.toString());
}
// Keys used in Hashmap
String[] from = { "country", "flag", "author" };
// Ids of views in listview_layout
int[] to = { R.id.tv_bookName, R.id.list_image, R.id.tv_bookAuthor };
// /////////
/*
* for (int i = 0; i < BookJsonParser.ids.length; i++) {
* System.out.println(BookJsonParser.ids[i]); }
*/
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
countries, R.layout.item_lv_layout, from, to);
return adapter;
}