0

I am currently creating a chat application. I tried different solutions and I could not get them to work. Here is my current code:

public void callListView() {
    // GET USER LIST
    setListAdapter (new SimpleAdapter(
        Messages.this, ListOfMsg,
        R.layout.messages_list_item, new String[] {
            "name",
            "message"
        },
        new int[] {
            R.id.sname,
            R.id.message
        }
    ));
}

How do I limit the data I get up to 25 items only? And what will I do to retrieve more data when I am scrolling up?

Thanks in advance.

UmAnusorn
  • 10,420
  • 10
  • 72
  • 100
Akio
  • 134
  • 1
  • 3
  • 13
  • 3
    Possible duplicate of [endless scroll list view](http://stackoverflow.com/questions/20324258/endless-scroll-list-view) – Yury Fedorov Jun 30 '16 at 07:17
  • Just get 25 items first. then get new items when u reach bottom. – Zoedia Jun 30 '16 at 07:19
  • Already Answered here http://stackoverflow.com/questions/16398921/dynamic-listview-adding-load-more-items-at-the-end-of-scroll – Siddhesh Dighe Jun 30 '16 at 07:20
  • if you accessing data from local database you can try query with OFFSET and LIMIT, it will give you limited data then In ListView scrollListener you can select next limited data and add to listview – Sachin Jun 30 '16 at 07:20

3 Answers3

0
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);
Touhidur
  • 205
  • 3
  • 8
0

You can access limited Data using following query

SELECT column1, column2, columnN 
FROM table_name
LIMIT [no of rows] OFFSET [row num]

Then In

listView.setOnScrollListener  

On Scroll Method

 public void onScroll(AbsListView view, int firstVisibleItem,
                        int visibleItemCount, int totalItemCount) {

  if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) {

//here you can fire query to get next data and attached to listview
}
}

you can get next data from above query and add to listview like pagination.

Sachin
  • 1,307
  • 13
  • 23
0

I used RecyclerView OnScrollListener.

LinearLayoutManager llm;
ArrayList posts;

And Then:

RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        if (!loading && llm.getChildCount() != 0 && 
             llm.findFirstCompletelyVisibleItemPosition() !=0 && //to ensure that the last item and the first item are not both visible   
             llm.findLastVisibleItemPosition() == posts.size() - 1) {

                  //Load Here Your Next Twenty Five Chat
        }
        super.onScrollStateChanged(recyclerView, newState);
    }

};

Then add the listener to RecyclerView as below

recycler.addOnScrollListener(scrollListener);

Hope it works.. (:

Zoedia
  • 295
  • 2
  • 13