我在AsyncTask的后台添加项目到ArrayList。在PostExecute上我创建了一个自定义适配器,我传入了ArrayList。
当我想添加更多项目时,ListView不会刷新。到目前为止,我有:
private class ProgressBlogList extends AsyncTask<String, Integer, String> {
ProgressBar blog_progress;
ProgressBar laProgress = new ProgressBar(getActivity(), null, android.R.attr.progressBarStyle);
ArrayList<Integer> posts = new ArrayList<Integer>();
@Override
protected void onPreExecute() {
super.onPreExecute();
blog_lista = (ListView) getView().findViewById(R.id.blog_list);
blog_progress = (ProgressBar) getView().findViewById(R.id.blog_progress);
if (blog_lista.getVisibility() == View.VISIBLE) {
blog_lista.addFooterView(laProgress);
}
}
@Override
protected String doInBackground(String... strings) {
DatabaseHelper db = new DatabaseHelper(getActivity());
arraydir = db.getTenItemBlogList(arraydir, id_number);
for(int i = 0; i<arraydir.size(); i++){
Integer post = arraydir.get(i).getId_post();
posts.add(post);
}
arrayImages = db.getImagePost(posts);
id_number = arraydir.size();
db.close();
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
AdapterBlogList adapter = new AdapterBlogList(getActivity(), arraydir,arrayImages);
blog_lista.setAdapter(adapter);
adapter.notifyDataSetChanged();
index = blog_lista.getFirstVisiblePosition();
View v = blog_lista.getChildAt(0);
top = (v == null) ? 0 : v.getTop();
blog_lista.setVisibility(View.VISIBLE);
blog_lista.removeFooterView(laProgress);
blog_progress.setVisibility(View.GONE);
}catch (Exception e) {
Log.e(e.getMessage(), "Error IVAN::");
}
blog_lista.setSelectionFromTop(index, top);
blog_lista.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (footerProgress != null) {
if (footerProgress.getStatus() != Status.RUNNING) {
footerProgress = new ProgressBlogList();
footerProgress.execute();
}
}else{
footerProgress = new ProgressBlogList();
footerProgress.execute();
}
}
}
});
blog_lista.setSelectionFromTop(index, top);
}
}