Pull to refresh returns the first position of the list

339 views Asked by At

I have one problem with pull to refresh, I use com.handmark.pulltorefresh. And I need to add new elements to this list when I see the last element of the list. I make this:

view = inflater.inflate(R.layout.fragment_generalnews, container,
                    false);
            mPullRefreshListView = (PullToRefreshListView) view
                    .findViewById(R.id.pull_refresh_list2);

....
// Add an end-of-list listener
            mPullRefreshListView
                    .setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

                        @Override
                        public void onLastItemVisible() {
                            if (GeneralActivity.razr <= 7.0) {
                                pagenumber += 1;
                                Log.d("Menu", "Page2 = " + pagenumber);
                                newpage = "page/" + pagenumber + "/";
                                final int position = mPullRefreshListView.getRefreshableView().getFirstVisiblePosition()+1;
                                new MyTask().execute();
                                Log.d("List", "position of list = " + position);
                                mPullRefreshListView.onRefreshComplete();
                                mPullRefreshListView.post(new Runnable() {

                                    @Override
                                    public void run() {
                                        // TODO Auto-generated method stub
                                        mPullRefreshListView.getRefreshableView().setSelection(position);
                                        kfulist.setSelection(position);
                                    }
                                });
                                Log.d("List", "List " + kfunews.toString());
                            }
                        }
                    });

In MyTask() in onPostExecute I refresh adapter:

kfulist = mPullRefreshListView.getRefreshableView();
                    kfulist.setAdapter(newsadapter);
                    kfulist = mPullRefreshListView.getRefreshableView();

                    // Need to use the Actual ListView when registering
                    // for Context Menu
                    registerForContextMenu(kfulist);
                    newsadapter.notifyDataSetChanged();

when I get to the end of the list, then it is updated and add new elements, but then returns to the first element of the list. And I can not understand why.

1

There are 1 answers

0
ToYonos On BEST ANSWER

We are talking about an AsyncTask here. As it's asynchronous, when mPullRefreshListView.onRefreshComplete() appends afer the execution of the task, this task is in fact not yet finished.

You should add in onPostExecute everything you want to do affecting the UI (onPostExecute is executed in the UI thread)

Try this :

public void onPostExecute(**whatever the parameter**)

  { kfulist = mPullRefreshListView.getRefreshableView(); kfulist.setAdapter(newsadapter); // Useless // kfulist = mPullRefreshListView.getRefreshableView();

    // Need to use the Actual ListView when registering
    // for Context Menu
    registerForContextMenu(kfulist);
    newsadapter.notifyDataSetChanged();

    Log.d("List", "position of list = " + position);
    mPullRefreshListView.onRefreshComplete();
    mPullRefreshListView.post(new Runnable()
    {
        @Override
        public void run()
        {
            mPullRefreshListView.getRefreshableView().setSelection(position);
            kfulist.setSelection(position);
        }
    });
    Log.d("List", "List " + kfunews.toString());     

    // [...]
}