Load More Item in lazy adapter and listview

不打扰是莪最后的温柔 提交于 2019-12-12 01:38:33

问题


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 ?

Here is my source code:

OnCreate Method in MainActivity Class

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();
               }
           }
        }
    });

}

LoadContent() Method in MainActivity Method:

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

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

}

GetLatestPosts Method in Post Class

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() , I will call after 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.


回答1:


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);

To

            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);


来源:https://stackoverflow.com/questions/24065321/load-more-item-in-lazy-adapter-and-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!