Reputation: 7582
List view is refreshed every N minutes of interval using a web service call. After refresh my current position goes back to the first item in list view.
Here is my code.
private void updateListUI(String response){
arrList = new ArrayList<Object>(); // to store array list
Object obj;
obj = some data; // some parsing of data and store in obj instance
arrList.add(obj);
if(listview.getAdapter()==null){
adapter = new customAdapter(myActivity,layout,arrList);
listview.setAdapter(adapter);
}else{
HeaderViewListAdapter adapterwrap =
(HeaderViewListAdapter) listview.getAdapter();
BaseAdapter basadapter = (BaseAdapter) adapterwrap.getWrappedAdapter();
customAdapter ad = (customAdapter)basadapter;
ad.setData(arrList); // it is a func in custom adapter that add items
ad.notifyDataSetChanged();
// here i want to set the position of list view,as it goes back to first item
}
}
BroadcastReceiver serviceReceiver = new BroadcastReceiver() {
public void onReceive( final Context context, final Intent intent ) {
updateListUI(intent.getStringExtra(response_from_service));
}
}
// Intent service calls webservice and broadcast response to receiver as finishes call. My question is on every updateListUI call list view goes back to first position. Is any way to solve this..?
Upvotes: 3
Views: 2350
Reputation: 717
use listLead.setSelectionFromTop(index, top); to set the position in list view
Upvotes: 2
Reputation: 4761
Just use the method notifyDataSetChanged() at the point where you want to refresh your list view.
Upvotes: 0
Reputation: 5492
Instead of loading setAdapter during refresh
use the below code ... it will work
yourAdapterName.notifyDataSetChanged();
Upvotes: 0
Reputation: 101
You can restore the posistion of your ListView using:
int position = 12; // your position in listview
ListView listView = (ListView) findViewById(R.id.listView);
listView.setSelectionFromTop(position, 0);
Upvotes: 2