android - 从自定义适配器更新后 ListView 回到第一个位置

标签 android listview

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/

相关文章:

java - 如何随着声音振动

javascript - parse.com REST API 使用 build.phonegap Javascript 推送通知 Android 和 iOS

Android 拇指不在搜索栏中心?

android - 将动态值添加到 Fragment 中的 ListView

python - Django ListView __init__ 接受关键字参数

javascript - jquery 样式未应用于动态创建

java - 如何删除java android app目录中的所有文件?当前代码正在删除该文件夹

android - 重定向到通知访问设置

android - GridLayout 内的 ListView

android - 如何在onPostExecute Asynctask中设置List Adapter?