getLastVisiblePosition returning -1

前端 未结 3 1468
傲寒
傲寒 2020-12-06 05:36

I\'m having a problem with my ListView (using CursorAdapter). When I call getListView().getLastVisiblePosition() I am receiving

相关标签:
3条回答
  • 2020-12-06 05:50

    My guess is that your getItem(int position) and getItemId(int position) methods aren't defined correctly in your adapter.

    0 讨论(0)
  • 2020-12-06 06:00

    This is because the moment you call getListView().getLastVisiblePosition() the listview is not rendered. You can add the call to the view's message queue like this:

    listview.post(new Runnable() {
        public void run() {
            listview.getLastVisiblePosition();
        }
    });
    
    0 讨论(0)
  • 2020-12-06 06:08

    Even I faced the same problem. The thing is that, getLastVisiblePosition() works only when you are in the first element of your listview and for the rest you will get null being returned. So what I did is that, I made a small calculation to find out the exact view position which I have mentioned below,

    int  LastPos=(mylist.getLastVisiblePosition()-mylist.getFirstVisiblePosition());
    

    This returns the exact last position without a doubt.

    0 讨论(0)
提交回复
热议问题