BaseAdapter造成的ListView走出去的顺序滚动时 [英] BaseAdapter causing ListView to go out of order when scrolled

查看:93
本文介绍了BaseAdapter造成的ListView走出去的顺序滚动时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,我从一本书改编的一些BaseAdapter code。我一直在使用这个code的变化遍布在我的应用程序的地方,但只滚动一个长长的清单时,在ListView的项目变得混乱,而不是所有的元素都显示刚刚意识到。

这是很难形容的具体行为,但它很容易看到,如果你采取的50项排序列表,并开始向上和向下滚动。

 类ContactAdapter扩展了BaseAdapter {

    ArrayList的<联系与GT; mContacts;

    公共ContactAdapter(ArrayList中<联系与GT;联系人){
        mContacts =联系人;
    }

    @覆盖
    公众诠释getCount将(){
        返回mContacts.size();
    }

    @覆盖
    公共对象的getItem(INT位置){
        返回mContacts.get(位置);
    }

    @覆盖
    众长getItemId(INT位置){
        返回的位置;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        查看图。
        如果(convertView == NULL){
            LayoutInflater李= getLayoutInflater();
            鉴于= li.inflate(R.layout.groups_item,NULL);
            TextView的标签=(TextView中)view.findViewById(R.id.groups_item_title);
            label.setText(mContacts.get(位置).getName());
            标签=(TextView中)view.findViewById(R.id.groups_item_subtitle);
            label.setText(mContacts.get(位置).getNumber());
        }
        其他
        {
            鉴于= convertView;
        }
        返回查看;
    }

}
 

解决方案

您只投入了的TextView 小部件数据首先被创建时。您需要将这些四行:

 的TextView标签=(TextView中)view.findViewById(R.id.groups_item_title);
        label.setText(mContacts.get(位置).getName());
        标签=(TextView中)view.findViewById(R.id.groups_item_subtitle);
        label.setText(mContacts.get(位置).getNumber());
 

如果 / 其他块之后,该方法返回之前,所以你更新的TextView 小部件是否正在回收行或创建一个新的。

I'm having problems with some BaseAdapter code that I adapted from a book. I've been using variations of this code all over the place in my application, but only just realized when scrolling a long list the items in the ListView become jumbled and not all of the elements are displayed.

It's very hard to describe the exact behavior, but it's easy to see if you take a sorted list of 50 items and start scrolling up and down.

class ContactAdapter extends BaseAdapter {

    ArrayList<Contact> mContacts;

    public ContactAdapter(ArrayList<Contact> contacts) {
        mContacts = contacts;
    }

    @Override
    public int getCount() {
        return mContacts.size();
    }

    @Override
    public Object getItem(int position) {
        return mContacts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if(convertView == null){
            LayoutInflater li = getLayoutInflater();
            view = li.inflate(R.layout.groups_item, null);
            TextView label = (TextView)view.findViewById(R.id.groups_item_title);
            label.setText(mContacts.get(position).getName());
            label = (TextView)view.findViewById(R.id.groups_item_subtitle);
            label.setText(mContacts.get(position).getNumber());
        }
        else
        {
            view = convertView;
        }
        return view;
    }

}

解决方案

You are only putting data in the TextView widgets when they are first created. You need to move these four lines:

        TextView label = (TextView)view.findViewById(R.id.groups_item_title);
        label.setText(mContacts.get(position).getName());
        label = (TextView)view.findViewById(R.id.groups_item_subtitle);
        label.setText(mContacts.get(position).getNumber());

to be after the if/else block and before the method return, so you update the TextView widgets whether you are recycling the row or creating a fresh one.

这篇关于BaseAdapter造成的ListView走出去的顺序滚动时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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