9

i have a listview with 100 items and i want to display first 10 items and on click of next button i have to display next 10 ie.,from 11 to 20, i have the code for getting first 10 items

public int getCount() {
        return 10;
    }

but how to get next 10 items alone and so on. any idea

saran
  • 461
  • 1
  • 6
  • 20

2 Answers2

8

One way to do this is,

1) Don't populate your ArrayList with all the data. Instead keep them in a separate ArrayList (al1) and use an ArrayList (al2) with max 10 values to use with your BaseAdapter.

2) Initially,

  • al2 = al1[0] to al1[9]
  • BaseAdatper(context, data)

3) Keep the BaseAdapter as it is but change

 @Override
 public int getCount() {
 return 10;
}

to

 @Override
 public int getCount() {
 return data.size();
}

It's not a must but it's good practice. Now you'll be showing only 10 items cause that's all you are passing to the adapter. Also write a public function in your extended BaseAdatper class to set the data variable.

4) On the next button click event get the next 10 items from al1 and assign to al2. Use the public function you wrote to over write data with al2.

5) BaseAdapter has a method called notifyDataSetChanged , call it. What this does is tell the adapter to refresh from top to bottom. Since you have data over written with new data when the refresh occurs you'll be seeing the new data. That's it.

I don't think it'll be difficult for you to come up with a way to keep track from which index to which in al1 you're currently displaying. :)

Ranjit
  • 5,130
  • 3
  • 30
  • 66
JanithaR
  • 1,094
  • 2
  • 11
  • 23
  • how to get al2 = al1[0] to al1[9] because im using like this arraylist of products ie., ArrayList array_sort – saran Feb 14 '13 at 09:48
  • I'm not sure of what you're asking. Are you asking how to get values from - to an index or how to keep track of the next index you should grab data from `al1` ? – JanithaR Feb 14 '13 at 10:02
  • `// class level variable currIndex = 0; onNextButtonClick() { for(var i = currIndex; i < 10; i++) { // have a condition to check al1.get(i) is not going out of bounds al2.add(al1.get(i)); currIndex++; } }` – JanithaR Feb 14 '13 at 10:10
  • thanks logic is corect but since im using ArrayList i m getting some error – saran Feb 14 '13 at 10:36
  • @JanithaR can u give me some example for the above process. Am also facing the same issue. – AndroidOptimist Oct 08 '13 at 05:40
3

If you have 100 items, then just take the first 10 items for your adapter, and when the user presses next, get the next 10, and so on.

EDIT: On request for code, I can provide a simple example on how to do pagination.

int totalItems = 100;
int currentPage = 0;
int pageSize = 10;
int numPages = (int) Math.ceil((float) totalItems/pageSize);

ArrayList<String> items = new ArrayList<String>(totalItems);

List<String> page = items.subList(currentPage, pageSize);

Looking at the example above, given the number of items and the desired page size you can calculate how many pages you need to display, you can then select a sub list from your ArrayList. Each time the user presses next, increment the currentPage and refresh the adapter with the a new sub list.

Ian Warwick
  • 4,774
  • 3
  • 27
  • 27
  • 1
    Ok, but you need to first post what code you have, are you using an array list or something else for your data source? – Ian Warwick Feb 14 '13 at 07:29
  • i am using arraylist to store the data and to display and slao using adapter class which extends BaseAdapter – saran Feb 14 '13 at 07:33
  • this code generating exception if there is 12 record after for (int i = 0; i splitData = arrayDetail.subList(currentPage, pageSize); myModel.add(splitData); } – Abdul Rahman Majeed Jun 02 '16 at 13:22