user1910290
user1910290

Reputation: 537

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)

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

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. how do I notifiy the adapter that more data has been added or data has been changed Thanks!.

EDIT: 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

Upvotes: 0

Views: 2217

Answers (2)

user1910290
user1910290

Reputation: 537

Finally! for those who are stuck with the same problem, I have the solution.

I have noticed that adapter.notifyDataSetChanged(); does not work if you are using String[] arrays to store data which is displayed into the listview rows.

Instead if you use List<String> list = new ArrayList<String>();and store data in the the listview will be updated with new data :)

Upvotes: 2

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21191

you can check this github project if you have any problem with your previous code.

otherwise if you want to load data like pagination you can try this example and this one

Upvotes: 1

Related Questions