从自定义适配器更新后,列表视图回到第一个位置

3

使用 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
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调用时,列表视图都会回到第一个位置。有没有解决这个问题的方法..?


1
这里的问题是什么? - Andro Selva
4个回答

2
您可以使用以下方法恢复ListView的位置:
int position = 12; // your position in listview
ListView listView = (ListView) findViewById(R.id.listView);
listView.setSelectionFromTop(position, 0);

2
使用listLead.setSelectionFromTop(index, top);来设置列表视图中的位置。

0

在刷新时不要加载setAdapter

使用以下代码...它会起作用

yourAdapterName.notifyDataSetChanged();

0
在编程中,只需在需要刷新列表视图的位置使用方法 notifyDataSetChanged() 即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接