lundi 28 mars 2016

How to implement ListView Pagination from SQLite database in Android?

I want to implement List-View from SQLite Database that i have done. But right now i want to implement pagination on List View setOnScrollListener.First time I'm displaying first 50 records after that when I scroll at the end of list run the progress bar some amount of second and after stop the progress bar another next 50 records append to the list.I'm trying lot of time since 5 days working on List View Pagination but it is not working properly.Can some one help me to resolve this issue.

Here is my code .

listView.addFooterView(footer);
        // Implementing scroll refresh
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
            }

            @Override
            public void onScroll(AbsListView absListView, int firstItem, int visibleItemCount, final int totalItems) {
                //Log.e("Get position", "--firstItem:" + firstItem + "  visibleItemCount:" + visibleItemCount + "  totalItems:" + totalItems + "  pageCount:" + pageCount);
                int total = firstItem + visibleItemCount;

               Log.e("", "onScroll LocalPages=" + LocalPages);
                // Total array list i have so it
                if (pageCount < LocalPages) {

                    if (total == totalItems) {

                        // Execute some code after 8 seconds have passed
                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable()
                        {
                            public void run()
                            {
                                listView.addFooterView(footer);
                                OFFSET = pageCount * 50 ;
                                //Log.e("","After OFFSET pageCount =" + pageCount + "  OFFSET value ="+OFFSET);
                                List<All_Post> allDesc = dbhelper.getAllDescriptions(OFFSET);
                                for (All_Post all_Post : allDesc)
                                {
                                    descArray.add(all_Post);
                                }
                                if(adapter != null)
                                {
                                    adapter.notifyDataSetChanged();
                                    listView.setAdapter(adapter);
                                    listView.setSelection(totalItems);

                                    pageCount += 1;
                                    Log.e("","  pageCount =" + pageCount + " LocalPages="+LocalPages);

                                }
                            }
                        }, 2000);
                    }
                } else {
                    Log.e("hide footer", "footer hide");
                    listView.removeFooterView(footer);
                }
            }
        });

Aucun commentaire:

Enregistrer un commentaire