0

Hi am working on an android application. And am using a listview in some of my activities.

The problem is all of my listviews displayed are much longer so that the user needs to scroll the whole list to go for the last item.

Am trying to implement a pagination for this, like at first say only 20 items need to displayed on the listview. And at the end of my listview i need a titlebar which have next & previous buttons and on clicking on next button the listview will load the next records from 21st to 40 and so on.

Am using java rest webservice to load the listview.

Can anyone give me a good suggestion for solving my problem.?

TKV
  • 2,533
  • 11
  • 43
  • 56

4 Answers4

1

Solution 1:

You can load all the data at once if its not TOO MUCH, store it locally & then you can navigate in that locally stored data. Define some variables like StartPoint & EndPoint & get the desired data from that stored data. Increment decrement the values of StartPoint & EndPoint by using the PreviouButton & NextButton.

Solution 2:

Get only the desired data from your data source for example 10 records each time when a Navigation button is clicked.

Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
  • i know these are the things i need to do for implementing pagination. But the i want to know is how i can done this in android. – TKV Feb 09 '12 at 10:30
0

The easiest solution is to add a footer view to the listview. And on the item click listener you can see if it is the last position (load more items), or not

//add the footer before adding the adapter, else the footer will not load!

View footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);

this.getListView().addFooterView(footerView);
oriolpons
  • 1,883
  • 12
  • 20
0

I suggest than you load list data in a custom Adapter class that extends BaseAdapter class. Like @oriolpons suggested, you should add a footer view, and when you click on button next call some method that is fetching next for example 20 rows, and then add them in your adapter object and call notifyDataSetChanged().

For example

private OnClickListener mListener = new OnClickListener() {

    public void onClick(View v) {
        ArrayList<YourObject> al = getSomeData(int startRow, int endRow);
                    MyCustomAdapter adapter = new MyCustomAdapter();
                    for(YourObject a : al)
                       adapter.add(a);
                       getListView.setAdapter(adapter);
                       notifyDataSetChanged();
    }
};

Hope this helps.

luciferche
  • 134
  • 9
-1

@Tijo . Refer this site http://www.androidhive.info/2012/03/android-listview-with-load-more-button/. You can have a button which would call the execute method of Async task and that will load the remaining list for you.

Nishit
  • 63
  • 1
  • 7