Android的 - 返回列表更新后顶部 [英] Android - list returns to top after update

查看:179
本文介绍了Android的 - 返回列表更新后顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被通过处理程序postDelayed()方法,每2秒刷新列表。

I have a list that gets refreshed every 2 seconds via the Handler postDelayed() method.

每2秒的AsyncTask运行,使一个HTTP GET请求,打开成JSON对象的列表,然后设置ListAdapter:

Every 2 seconds an AsyncTask is run that makes an HTTP GET request, turns the JSON into a list of objects, then sets the ListAdapter:

 MyListAdapter adapter = new MyListAdapter(someObjects);
 setListAdapter(adapter);

我的问题是,每次任务完成时间(所以,每两秒钟大约)我的名单跳回到顶端,即使我已经向下滚动到列表中的中间或底部。这将是很烦人给最终用户,所以需要在列表中的背景更新,因为它的操作的,但是该列表的当前视图不是在的AsyncTask完成跳回到顶部。

My problem is that every time the task completes (so, roughly every two seconds) my list jumps back to the top, even if I have scrolled down to the middle or bottom of the list. This would be very annoying to the end user, so I need the list to update in the background, as it's doing, but the current view of the list to not jump back to the top at the completion of the AsyncTask.

我可以包括需要更多的code。我有点新的android开发,所以我不知道什么是帮助他人。

I can include any more code needed. I'm somewhat new to android development, so I'm not sure what is helpful to others.

其他信息

从服用建议hacksteak25,我能够得到的地方,我尝试从适配器删除所有的数据点,则一次添加回一个对象。这是不是因为这个最终解决方案很可能仍然会造成画面跳转,但我想用它作为概念的,我怎么能在某个时候合并的数据的证明。

Taking suggestions from hacksteak25, I'm able to get to the point where I try to remove all the data from the adapter, then add it back one object at a time. This isn't the end solution since this would probably still cause the screen to jump, but I'm trying to use it as a proof of concept for how I could merge the data at some point.

我的问题是,我拨打以下code:

My problem is that I call the following code:

 MyListAdapter adapter = (MyListAdapter)getListAdapter();
 adapter.clear();
 for(MyObject myObject : myObjects)
 {
  adapter.add(myObject);
 }

在第一次调用添加(myObject的)的MyListAdapter的getView()方法被调用。自定义适配器的专用内部ArrayList是空在这一点上,无论是因为我的onCreate(设置适配器没有myObjects)还是因为我叫明()在适配器上,我不知道。无论哪种方式,这将导致getView失败,因为有ArrayList中没有对象要充分利用视图。

After the first call to "add(myObject)" the getView() method of the MyListAdapter is being called. The private internal ArrayList of the custom adapter is empty at this point, either because I set the adapter with no myObjects in onCreate() or because I called clear() on the adapter, I'm not sure. Either way, this causes getView to fail since there are no objects in the ArrayList to be getting the view from.

getView()看起来是这样的:

getView() looks like this:

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之一。

That last line is the one that causes an IndexOutOfBoundsException.

我应该如何处理这种情况下,我得到了我想要在那里的数据,而不会导致列表跳?

How should I handle this situation where I get the data I want in there without causing the list to jump?

推荐答案

我觉得preferred办法是更新适配器本身,而不是取代它。也许你可以编写合并使用的适配器新老数据插入()删除()方法的方法。我认为应该保持你的立场。

I think the preferred way is to update the adapter itself and not to replace it. Maybe you can write a method that merges the new and old data using the adapters insert() and remove() methods. I think that should keep your position.

补充信息:

我用下面的基本结构。也许这会有所帮助。

I use the following as basic structure. Maybe it helps.


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;
    }

}

这篇关于Android的 - 返回列表更新后顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆