2

I have a long ListView that the user can scroll around. I am refreshing this listview after every 30 seconds, listview refresh succesfully. But there is one issue that I want the list to be scrolled to the same point that it was previously before refreshing. Because now if list refresh then it shows the first position.

I am updating list through AsynchronousTask as:

private class HttpTask extends AsyncTask<String, Boolean, String> {

    @Override
    protected String doInBackground(String... params) {
        System.out.println("inside doInBackground>>!!>>> ");
        getResponse();

        return "Executed";
    }

    @Override
    protected void onProgressUpdate(Boolean... progress) {

    }

    @Override
    protected void onPostExecute(String result) {
        // publishProgress(false);
        System.out.println("post excute funt***********");
        setListAdapter(new ListAdapter(getApplicationContext(),
                R.layout.mylist, dataArr));
    }

}

Any help will be appreciated.Thanks in advance

PPD
  • 5,660
  • 12
  • 52
  • 86
  • 1
    This may help you http://stackoverflow.com/questions/3014089/maintain-save-restore-scroll-position-when-returning-to-a-listview. Exact thing you want to do. – NaviRamyle Feb 08 '13 at 07:13

5 Answers5

2

try to use setSelectionFromTop() to set the position to the previously visible state.

Raj
  • 1,843
  • 2
  • 21
  • 47
  • 1
    Thanks by adding listView.setSelectionFromTop(startPosition, 0); solves my issue – PPD Feb 08 '13 at 08:07
1

You would just have to do this :

listView.setSelection(position) // position is the item on the listView
lokoko
  • 5,785
  • 5
  • 35
  • 68
1

I think when your listview is going to be refreshed the get the position of item which is first visible at that time then refresh your listview and set the position which you got before refreshing

    int firstPosition = mEventListView.getFirstVisiblePosition();
    EventLogAdapter eventLogAdapter = new EventLogAdapter(mContext, events);
    mEventListView.setAdapter(eventLogAdapter);
    mEventListView.setSelection(firstPosition);
j0k
  • 22,600
  • 28
  • 79
  • 90
Milan Shukla
  • 1,602
  • 18
  • 16
0

Instead of set a new ListAdapter, do listAdapter.notifyDataSetChanged() this will refresh the contents of view instead of create new ones and the listview will not scroll up

PaNaVTEC
  • 2,505
  • 1
  • 23
  • 36
0

What you are doing is you are assigning adapter to your list view when you call refresh

setListAdapter(new ListAdapter(getApplicationContext(),
            R.layout.mylist, dataArr));

Which is wrong

You have to refresh your adapter

dataArr.notifyDataSetChanged();

Hope this will work

Umer Abid
  • 397
  • 1
  • 14