Android-更新后列表返回顶部

Android-更新后列表返回顶部,android,user-interface,listview,android-asynctask,android-arrayadapter,Android,User Interface,Listview,Android Asynctask,Android Arrayadapter,我有一个列表,它通过Handler postdayed()方法每2秒刷新一次 每2秒运行一个AsyncTask,发出HTTP GET请求,将JSON转换为对象列表,然后设置ListAdapter: MyListAdapter adapter = new MyListAdapter(someObjects); setListAdapter(adapter); 我的问题是,每当任务完成时(大约每两秒钟),我的列表就会跳回顶部,即使我已经向下滚动到列表的中间或底部。这对最终用户来说是非常烦人的,

我有一个列表,它通过Handler postdayed()方法每2秒刷新一次

每2秒运行一个AsyncTask,发出HTTP GET请求,将JSON转换为对象列表,然后设置ListAdapter:

 MyListAdapter adapter = new MyListAdapter(someObjects);
 setListAdapter(adapter);
我的问题是,每当任务完成时(大约每两秒钟),我的列表就会跳回顶部,即使我已经向下滚动到列表的中间或底部。这对最终用户来说是非常烦人的,所以我需要在后台更新列表,就像现在一样,但是列表的当前视图不能在任务完成时跳回顶部

我可以包含任何需要的代码。我对安卓系统的开发有些陌生,所以我不确定什么对其他人有帮助

其他信息

根据hacksteak25的建议,我可以尝试从适配器中删除所有数据,然后一次添加一个对象。这不是最终的解决方案,因为这可能仍然会导致屏幕跳转,但我正试图用它来证明我如何在某个时候合并数据

我的问题是我调用了以下代码:

 MyListAdapter adapter = (MyListAdapter)getListAdapter();
 adapter.clear();
 for(MyObject myObject : myObjects)
 {
  adapter.add(myObject);
 }
在第一次调用“add(myObject)”之后,将调用MyListAdapter的getView()方法。此时,自定义适配器的私有内部ArrayList为空,这可能是因为我在onCreate()中设置了没有MyObject的适配器,或者因为我在适配器上调用了clear(),我不确定。无论哪种方式,这都会导致getView失败,因为ArrayList中没有要从中获取视图的对象

getView()如下所示:

public View getView(int position, View convertView, ViewGroup parent)
{
 ViewHolder holder;
 LayoutInflater mInflater = getLayoutInflater();

 if (convertView == null)
 {
  convertView = mInflater.inflate(R.layout.myObject, null);

  holder = new ViewHolder();
  holder.someProperty = (TextView)convertView.findViewById(R.id.someProperty);
  holder.someOtherProperty = (TextView)convertView.findViewById(R.id.someOtherProperty);
  holder.someOtherOtherProperty = (TextView)convertView.findViewById(R.id.someOtherOtherProperty);

  convertView.setTag(holder);
 }
 else
 {
  holder = (ViewHolder)convertView.getTag();
 }

 // Bind the data efficiently with the holder.
 holder.someProperty.setText( mObjects.get(position).getSomeProperty());
 ...
最后一行是导致IndexOutOfBoundsException的行


我应该如何处理这种情况,在不导致列表跳转的情况下获取所需数据?

我认为首选的方法是更新适配器本身,而不是替换它。也许您可以编写一个方法,使用适配器
insert()
remove()
方法合并新数据和旧数据。我认为这应该保持你的立场

补充资料:

我使用以下内容作为基本结构。也许有帮助


public class PlaylistAdapter extends ArrayAdapter<Playlist> {

    private ArrayList<Playlist> items;

    public PlaylistAdapter(Context context, int textViewResourceId, ArrayList<Playlist> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

}

公共类播放适配器扩展了ArrayAdapter{
私有ArrayList项;
公共播放列表适配器(上下文上下文、int textViewResourceId、ArrayList项){
super(上下文、textViewResourceId、项);
这个项目=项目;
}
}

在尝试合并数据之前,我试着看看是否可以轻松地实现这一点。但是,下面的代码是:MyListAdapter=(MyListAdapter)getListAdapter();适配器。清除();对于(Object-Object:objects){adapter.add(Object);}会在java.util.AbstractList.add处产生java.lang.UnsupportedOperationException,这很奇怪,因为MyListAdapter扩展了ArrayAdapter,ArrayAdapter应该有add方法可用。“java.lang.UnsupportedOperationException位于java.util.AbstractList.add”:如何初始化超类?当我将适配器数据设置为数组(例如MyObject[]类型)时,我遇到了同样的问题。尝试使用列表(如ArrayList类型)。无法调整数组的大小,但可以调整列表的大小。这应该可以解决您的不支持操作异常。我试图将我的实现从使用MyObject[]更改为ArrayList。这需要一些重构,因为我使用的是扩展ArrayAdapter的自定义ListAdapter。在构造函数中,我使用ArrayList.toArray()来满足超级构造函数的要求,因为我找不到ArrayAdapter的合适替代品。无论哪种情况,我的应用程序都会运行,但数据现在根本不会显示。我可以展示哪些代码会有帮助?将toArray()值传递给超级构造函数将导致相同的问题。数组大小不能更改。我在答案中添加了一些代码。也许有帮助。你们班看起来怎么样?