我在JSON工作。我在一年级有两个类(私有类LoadDataToServer扩展了AsyncTask,loadMoreListView类扩展了AsyncTask),我在ListView中解析JSON并显示项,第二个类,我试图加载多个项。我的问题是当我调用新的loadMoreListView ()时。execute ();..在listView.setOnScrollListener上出现错误。我的问题是适配器这是我的代码
私有类loadMoreListView扩展了AsyncTask>> {
@Override
protected void onPreExecute() {
pd.show();
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(
Void... params) {
jsonparser = new JSONParser();
URL = "http://bri.ge/api/getList.aspx?count=2&time=" + dateTime;
jsonarray = new JSONArray();
JSONObject jsonobject = jsonparser.getJSONfromURL(URL);
try {
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("journal", jsonobject.getString(KEY_journal));
map.put("image", jsonobject.getString(KEY_image));
map.put("title", jsonobject.getString(KEY_title));
map.put("description",
jsonobject.getString(KEY_description));
map.put("JournalID", KEY_JournalID);
map.put("pubDate", jsonobject.getString(KEY_pubDate));
Content cont = new Content(jsonobject.getString("journal"),
jsonobject.getString("image"),
jsonobject.getString("title"),
jsonobject.getString("pubDate"),
jsonobject.getString("description"),
jsonobject.getInt("JournalID"));
contents.add(cont);
itemList.add(map);
}
// adapter.notifyDataSetChanged();
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
dateTime = itemList.get(itemList.size() - 1).get(KEY_pubDate);
return itemList;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
if (pd.isShowing()) {
pd.dismiss();
try {
// itemList.clear();
int currentPosition = list.getFirstVisiblePosition();
// Appending new data to menuItems ArrayList
adapter = new LazyAdapter(MainActivity.this, itemList);
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
// list.add
// Setting new scroll position
list.setSelectionFromTop(currentPosition + 1, 0);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
}私有类LoadDataToServer扩展了AsyncTask>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pd.show();
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(
Void... params) {
jsonparser = new JSONParser();
@SuppressWarnings("static-access")
JSONObject jsonobject = jsonparser.getJSONfromURL(URL);
try {
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("journal", jsonobject.getString(KEY_journal));
map.put("image", jsonobject.getString(KEY_image));
map.put("title", jsonobject.getString(KEY_title));
map.put("description",
jsonobject.getString(KEY_description));
map.put("JournalID", KEY_JournalID);
map.put("pubDate", jsonobject.getString(KEY_pubDate));
// contents = new ArrayList<Content>();
Content cont = new Content(jsonobject.getString("journal"),
jsonobject.getString("image"),
jsonobject.getString("title"),
jsonobject.getString("pubDate"),
jsonobject.getString("description"),
jsonobject.getInt("JournalID"));
contents.add(cont);
itemList.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
dateTime = itemList.get(itemList.size() - 1).get(KEY_pubDate);
return itemList;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
pd.dismiss();
// itemList.clear();
// Appending new data to menuItems ArrayList
adapter = new LazyAdapter(MainActivity.this, itemList);
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
// list.add
// Setting new scroll position
}
}新建(list.setOnScrollListener OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == 0) {
// new loadMoreListView().execute();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
final int lastItem = firstVisibleItem + visibleItemCount;
if (lastItem == totalItemCount) {
new loadMoreListView().execute();
}
}
});发布于 2013-12-14 02:20:44
我猜在你的网络调用(异步任务)中的某个地方,你的"itemList“在没有通知适配器的情况下得到了更新。因为您在不同线程上的"doInBackGround“中添加了项。
我建议您使用这两个库进行REST网络调用和json解析。这在内存和性能方面是高效的。
Android Asynchronous Http Client
这些都是开源的。如有任何疑问,请向我咨询。
发布于 2013-12-16 18:19:30
当您获得新数据时,在将数据设置为适配器后,请调用adapter.notifyDataSetChanged();这将通知列表适配器中的数据已更改
希望这能有所帮助
https://stackoverflow.com/questions/20572822
复制相似问题