p. ld p. ld - 4 months ago 21
Android Question

how to save List-view position when back to previous Activity in Android

I have a ListView with huge amount of data.But when i go to next page on ListView Item Click and come back to ListView. I want the list to be scrolled to the same point that it was previously.Can someone help to achieve this.

I tried this code.List to be scrolled to the same point but immediately come to the first position.

private void populateList() {
descArray.clear();
List<All_Post> allDesc = dbhelper.getAllDescriptions();
for (All_Post all_Post : allDesc)
{
descArray.add(all_Post);
}

if (adapter == null)
{
adapter = new AllPostAdapter(this, R.layout.allpostlist, descArray);
listView.setAdapter(adapter);
}
else if (adapter != null) {


adapter.notifyDataSetChanged();
adapter = new AllPostAdapter(this, R.layout.allpostlist, descArray);
listView.setAdapter(adapter);
//int firstPosition = listView.getFirstVisiblePosition();
//listView.setSelection(firstPosition);

int index = listView.getFirstVisiblePosition();
View v = listView.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop());
listView.setSelectionFromTop(index, top);
}
}


Thanks in advanced .

Answer

it is default property of listview.

if you move to next page on ListView Item Click and come back to ListView it will show same position.

just keep one thing in mind that, you should not refresh your listview on backpressed() or when you are coming back to listview or any otherway you are using to moveback to listview .

Comments