Android 从自定义适配器更新后,列表视图回到第一个位置
使用web服务调用每隔N分钟刷新一次列表视图。刷新后,我的当前位置返回列表视图中的第一项 这是我的密码Android 从自定义适配器更新后,列表视图回到第一个位置,android,listview,Android,Listview,使用web服务调用每隔N分钟刷新一次列表视图。刷新后,我的当前位置返回列表视图中的第一项 这是我的密码 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 arr
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));
}
}
private void updateListUI(字符串响应){
arrList=new ArrayList();//用于存储数组列表
对象对象对象;
obj=some data;//对数据进行一些解析并存储在obj实例中
arrList.add(obj);
if(listview.getAdapter()==null){
adapter=新的customAdapter(myActivity、layout、arrList);
setAdapter(适配器);
}否则{
HeaderViewListAdapter适配器RAP=
(HeadServiceWListAdapter)listview.getAdapter();
BaseAdapter=(BaseAdapter)AdapterRap.getWrappedAdapter();
customAdapter ad=(customAdapter)basadapter;
ad.setData(arrList);//它是自定义适配器中添加项的func
ad.notifyDataSetChanged();
//在这里,我想设置列表视图的位置,因为它返回到第一项
}
}
BroadcastReceiver serviceReceiver=新的BroadcastReceiver(){
公共void onReceive(最终上下文、最终意图){
updateListUI(intent.getStringExtra(来自服务的响应));
}
}
//意图服务调用webservice,并在调用完成时向接收方广播响应。我的问题是在每次更新时,调用列表视图都返回到第一个位置。有没有办法解决这个问题?您可以使用以下方法恢复ListView的位置:
int position = 12; // your position in listview
ListView listView = (ListView) findViewById(R.id.listView);
listView.setSelectionFromTop(position, 0);
而不是在刷新期间加载setAdapter 使用下面的代码。。。它会起作用的
yourAdapterName.notifyDataSetChanged();
只需在要刷新列表视图的位置使用方法notifyDataSetChanged()。使用listLead.setSelectionFromTop(index,top);要在列表视图中设置位置,这里有什么问题?