简体   繁体   English

将更多数据加载到ListView-Android

[英]Load more data into ListView - Android

I have implemented a listview in which data is loaded through an asynctask, To load more data I have used this code found here (exactly copy pasted) 我实现了一个listview,其中通过asynctask加载了数据,要加载更多数据,我使用了此处找到的代码(完全复制粘贴)

Main Activity 主要活动

class load_data extends AsyncTask<Integer, Void, Void>{

// This asynctask takes in a page number as argument and depending on that page number
// data is loaded and stored in various String[] arrays and their length is extended as     new items are loaded

protected void onPostExecute(Void result) {

if(adapter == null){
            adapter = new Listadapter(Main.this,String[],String[],String[]);
            list.setAdapter(adapter);

            }
            else if(adapter != null){

                adapter.notifyDataSetChanged();
            }

}
    }

This task is called as new load_data().execute(1); 此任务称为new load_data().execute(1);

after this code I have the load more data snippet from the above link everything works perfectly no syntax errors, and also the data loads after reacing the given threshold (20) in my case, however new data is not shown. 在这段代码之后,我从上面的链接中加载了更多数据片段,一切正常,没有语法错误,而且在我的情况下,重新达到给定的阈值(20)后,数据也加载了,但是未显示新数据。 how do I notifiy the adapter that more data has been added or data has been changed Thanks!. 如何通知适配器已添加更多数据或更改了数据,谢谢!

EDIT: LOADMORE CLASS 编辑:LOADMORE CLASS

class EndlessScrollListener implements OnScrollListener {

            private int visibleThreshold = 5;
            private int currentPage = 1;
            private int previousTotal = 0;
            private boolean loading = true;

            public EndlessScrollListener() {
            }

            public EndlessScrollListener(int visibleThreshold) {
                this.visibleThreshold = visibleThreshold;
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                if (loading) {
                    if (totalItemCount > previousTotal) {
                        loading = false;
                        previousTotal = totalItemCount;
                        currentPage++;
                    }
                }
                if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
                    // I load the next page of gigs using a background task,
                    // but you can call any function here.
                    new load_data().execute(currentPage);
                    adapter.notifyDataSetChanged();
                    loading = true;
                }
            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }
        }

list.setOnScrollListener(new EndlessScrollListener(20)); // 20 being the threshold

Finally! 最后! for those who are stuck with the same problem, I have the solution. 对于那些遇到相同问题的人,我有解决方案。

I have noticed that adapter.notifyDataSetChanged(); 我注意到那个adapter.notifyDataSetChanged(); does not work if you are using String[] arrays to store data which is displayed into the listview rows. 如果您使用String []数组存储显示在列表视图行中的数据,则无法正常工作。

Instead if you use List<String> list = new ArrayList<String>(); 相反,如果您使用List<String> list = new ArrayList<String>(); and store data in the the listview will be updated with new data :) 并在listview中存储数据将使用新数据进行更新:)

you can check this github project if you have any problem with your previous code. 如果您以前的代码有任何问题,可以检查此github项目

otherwise if you want to load data like pagination you can try this example and this one 否则,如果要加载喜欢的拼版数据,你可以尝试这个例子这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM