ListView 使用网络服务调用每隔 N 分钟刷新一次。刷新后我的当前位置回到 ListView 中的第一项。
这是我的代码。
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 服务调用 web 服务,并在调用结束时将响应广播给接收者。我的问题是每个 updateListUI 调用 ListView 都返回到第一个位置。有什么办法可以解决这个...?
最佳答案
使用 listLead.setSelectionFromTop(index, top);在 ListView 中设置位置
关于android - 从自定义适配器更新后 ListView 回到第一个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14154510/