Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – BaseAdapter导致ListView在滚动时出错大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我从一本书改编的一些BaseAdapter代码遇到问题.我一直在应用程序中使用这些代码的变体,但是只有在滚动长列表时才意识到,ListView中的项目变得混乱,并不是所有的元素都显示出来.

很难描述确切的行为,但是很容易看出你是否选择了50个项目的排序列表,并开始向上和向下滚动.

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

}

解决方法

当您首次创建TextView小部件时,您只会将数据放入.你需要移动这四行:
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());

在if / else块之后和方法返回之前,所以您可以更新TextView小部件,无论是回收该行还是创建新的行.

大佬总结

以上是大佬教程为你收集整理的android – BaseAdapter导致ListView在滚动时出错全部内容,希望文章能够帮你解决android – BaseAdapter导致ListView在滚动时出错所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。