How to limit list items display in ListView by 10 and next 10after clicking next button

后端 未结 2 1027
误落风尘
误落风尘 2020-12-29 12:48

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 fi

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 13:15

    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 items = new ArrayList(totalItems);
    
    List 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.

提交回复
热议问题