Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – ListView with ArrayAdapter和ViewHolder将图标添加到错误的项目大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用ArrayAdapter的动态ListView.从微调器中选择名称时,名称以及显示它们是男性还是女性的图标将@L_@R_772_11241@_5@到ListView中.

通常一切都很好(名称正确地@L_@R_772_11241@_5@到列表中,与图标一起).但是显示性别的图标会被@L_@R_772_11241@_5@到ListView中的错误项目中.名称将@L_@R_772_11241@_5@到列表的底部,但图标将放置在列表顶部的名称处.我不知道这是否是我使用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(@R_450_2196@,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 prev@R_419_2469@sly 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(@R_450_2196@,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;
}

大佬总结

以上是大佬教程为你收集整理的android – ListView with ArrayAdapter和ViewHolder将图标添加到错误的项目全部内容,希望文章能够帮你解决android – ListView with ArrayAdapter和ViewHolder将图标添加到错误的项目所遇到的程序开发问题。

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

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