I have a listview which loads data from server. The data loads properly, but after some time and scrolling the list the application crashes and I get the message "data recieved but adapter is not notified!" The following is my code:
private class LoadData extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
applicationList = new ArrayList();
applicationList.clear();
try {
JSONArray jsonData = new GetListviewsData().getJSONData(webfileName, limit, offset, priceCat);
for (int i = 0; i <= jsonData.length() - 2; i++) {
JSONObject c = jsonData.getJSONObject(i);
id = c.getString("id");
name = c.getString("name");
logo = c.getString("logo");
developer = c.getString("developer");
category = c.getInt("category");
fileName = c.getString("filename");
path = c.getString("path");
appSize = c.getDouble("size_bytes");
price = c.getInt("price");
data = c.getString("data1").equals("t");
obb = c.getString("data").equals("t");
applicationList.add(new ApplicationPojo(id, name, logo, developer, appSize, category, fileName, path, data, obb, price));
}
JSONObject sizeObj = jsonData.getJSONObject(jsonData.length() - 1);
listSize = sizeObj.getInt("size");
} catch (Exception ex) {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(listSize == 0){
TextView noResult = (TextView) findViewById(R.id.noresult);
noResult.setVisibility(View.VISIBLE);
mProgressDialog.dismiss();
}else{
adapter = new ListViewAppAdapter(ListViewAppAll.this, applicationList, listview);
listview.setAdapter(adapter);
mProgressDialog.dismiss();
listview.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
int threshold = 1;
int count = listview.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (listview.getLastVisiblePosition() >= count - threshold) {
if (listSize >= offset) {
offset = offset + 15;
new LoadMoreData().execute();
} else {
Toast.makeText(getApplicationContext(), "End of list!", Toast.LENGTH_LONG).show();
listview.removeFooterView(footer);
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
}
}
}
private class LoadMoreData extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
GetListviewsData getDataAppList = new GetListviewsData();
JSONArray jsonData = getDataAppList.getJSONData(webfileName, limit, offset, priceCat);
for (int i = 0; i <= jsonData.length(); i++) {
JSONObject c = jsonData.getJSONObject(i);
id = c.getString("id");
name = c.getString("name");
logo = c.getString("logo");
developer = c.getString("developer");
category = c.getInt("category");
fileName = c.getString("filename");
path = c.getString("path");
appSize = c.getDouble("size_bytes");
price = c.getInt("price");
data = c.getString("data1").equals("t");
obb = c.getString("data").equals("t");
applicationList.add(new ApplicationPojo(id, name, logo, developer, appSize, category, fileName, path, data, obb, price));
}
} catch (Exception ex) {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
adapter.notifyDataSetChanged();
}
}
please help me with this. Thanks
I made some changes to your code.
Changes,
applicationList
, add it all at once usingaddAll
and callnotifyDataSetChanged
. This should remove the error.adapter
inside oncreate.AsyncTask
instead of two.AsyncTask
doInbackground
returnsArrayList
instead ofvoid
.See the code below,