在惰性适配器和列表视图中加载更多项目

问题描述:

我将在列表视图中加载更多数据。我可以加载更多数据,但是它将删除以前的数据。

I am going to load more data in my list view. I can load more data but it will remove previous data.

我应该怎么做才能在列表视图中附加数据?

What should i do to append data in my list view ?

这是我的源代码:

MainActivity类中的OnCreate方法

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recentposts);

    lv = (ListView) findViewById(R.id.list);

    //LoadContent();

    lv.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {

           final int lastItem = firstVisibleItem + visibleItemCount;
           if (chkLoad == true) {
               if(lastItem == totalItemCount) {
                   chkLoad = false;
                   LoadContent();
               }
           }
        }
    });

}

MainActivity方法中的LoadContent()方法:

public void LoadContent(){
    lv = (ListView) findViewById(R.id.list);

    Post p = new Post(theCounter);
    theCounter+=20;
    if (p.GetLatestPosts(lv,MainActivity.this))
    {
        //"true"
    }

}

Post类中的GetLatestPosts方法

public boolean GetLatestPosts(ListView lv, Activity a) {
    AsyncLatestPostTaskCompleteListener<String> mycb = new AsyncLatestPostTaskCompleteListener<String>();
    final CallLatestPost task = new CallLatestPost(counter,a,mycb,lv);
    task.execute("executer");

    return true;
}

AsyncLatestPostTaskCompleteListener(),我将在Asynctask Compeleted完成后调用。

public class AsyncLatestPostTaskCompleteListener<T> {
    private PostAdapter adapter; 
    public void onTaskComplete(String result,ListView _list, Activity _activity) {
        String tmpStr = result;
        JSONArray ja_arr = null;
        try {

            if (tmpStr != "ERROR") {
                ja_arr = new JSONArray(tmpStr);
                JSONObject jo = null;

                HashMap<String, String> map = new HashMap<String, String>();


                for (int i=0; i < ja_arr.length(); i++){
                    jo = ja_arr.getJSONObject(i);
                    map = new HashMap<String, String>();
                    map.put("name", jo.getString("name"));
                    map.put("postid", jo.getString("IdPost"));
                    map.put("title", jo.getString("Title"));
                    map.put("date", jo.getString("Date"));
                    map.put("photo", jo.getString("photo"));
                    map.put("url", jo.getString("linkurl"));
                    map.put("desc", jo.getString("Description"));
                    jo.getString("Title") != null){
                        PostList.add(map); 

                }

                adapter = new PostAdapter(_activity, PostList);
                adapter.notifyDataSetChanged(); 

                if ( pa == null ) {
                    pa = adapter;
                } else {
                    // I Think, I shoud update Adapter Here...! 
                }

                pa.notifyDataSetChanged();

                _list.setAdapter(pa);




            } else {

                // ERROR
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.i("@@@==ERR", e.toString());
        }
    }
}

所以,我该如何解决我的问题。

So, How can i solve my Problem.

我看不到实例化 pa的位置,但问题是您每次都创建一个新适配器然后覆盖 pa。您需要将逻辑从以下位置上移:

I don't see where you are instantiating 'pa' but the problem is you create a new adapter everytime and then overwrite the 'pa'. You need to move your logic up from:

adapter = new PostAdapter(_activity, PostList);
            adapter.notifyDataSetChanged(); 

            if ( pa == null ) {
                pa = adapter;
            } else {
                // I Think, I shoud update Adapter Here...! 
            }

            pa.notifyDataSetChanged();

            _list.setAdapter(pa);

            if ( pa == null ) {
                adapter = new PostAdapter(_activity, PostList);
                pa = adapter;
            }else{
                pa.append(PostList); //You would need to create this method.
             }

            pa.notifyDataSetChanged();

            _list.setAdapter(pa);