android – ListView with ArrayAdapter和ViewHolder将图标添加到错误的项目

前端之家收集整理的这篇文章主要介绍了android – ListView with ArrayAdapter和ViewHolder将图标添加到错误的项目前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用ArrayAdapter的动态ListView.从微调器中选择名称时,名称以及显示它们是男性还是女性的图标将添加到ListView中.

通常一切都很好(名称正确地添加到列表中,与图标一起).但是显示性别的图标会被添加到ListView中的错误项目中.名称添加到列表的底部,但图标将放置在列表顶部的名称处.我不知道这是否是我使用ViewHolder的方式,但在Android website中没有文档.

// Listview inflater
inflater = (LayoutInflater) (this).getSystemService(LAYOUT_INFLATER_SERVICE);

// List Array.
mAdapter = new ArrayAdapter<String>(this,R.layout.player_simple_list,R.id.label,mStrings) {

    @Override
    public View getView(int position,View convertView,ViewGroup parent) {

        Log.i("ANDY","View getView Called");
        // A ViewHolder keeps references to children views to 
        // avoid unneccessary calls to findViewById() on each row.
        ViewHolder holder;

        if (null == convertView) {
            Log.i("ANDY","Position not prevIoUsly used,so inflating");
            convertView = inflater.inflate(R.layout.player_simple_list,null);
            // Creates a ViewHolder and store references to the
            // two children views we want to bind data to.
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.label);
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            if (sexmale == true) {
                holder.icon.setImageBitmap(maleicon);
            }
            else {
                holder.icon.setImageBitmap(femaleicon);
            }
            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();

        }
        // Bind the data efficiently with the holder.
        holder.text.setText(getItem(position));
        // Change icon depending is the sexmale variable is true or false.
        Log.i("ANDY","getCount = "+mAdapter.getCount());
        return convertView;
    }
};
setListAdapter(mAdapter);

解决方法

您必须在if-else-if之后设置图标以创建或绑定持有者.否则,图标将仅在列表中的前几个项目中正确显示,即直到未填充ListView.
public View getView(int position,ViewGroup parent) {

    Log.i("ANDY","View getView Called");
    // A ViewHolder keeps references to children views
    // to avoid unneccessary calls to findViewById() on each row.
    ViewHolder holder;

        if (null == convertView) {
            Log.i("ANDY",null);

            // Creates a ViewHolder and store references to
            // the two children views we want to bind data to.
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.label);
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();

        }
        // Bind the data efficiently with the holder.
        holder.text.setText(getItem(position));

        // Change icon depending is the sexmale variable is true or false.
        if (sexmale == true) {
            holder.icon.setImageBitmap(maleicon);
        }
        else {
            holder.icon.setImageBitmap(femaleicon);
        }
        Log.i("ANDY","getCount = "+mAdapter.getCount());
        return convertView;
}
原文链接:https://www.f2er.com/android/317882.html

猜你在找的Android相关文章