带有 ArrayAdapter 和 ViewHolder 的 ListView 将图标添加到错误的项目 [英] ListView with ArrayAdapter and ViewHolder adding icons to the wrong item

查看:12
本文介绍了带有 ArrayAdapter 和 ViewHolder 的 ListView 将图标添加到错误的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 ArrayAdapter 的动态 ListView.当从微调器中选择一个名字时,该名字和一个显示他们是男性还是女性的图标会被添加到 ListView.

基本上一切都很好(名称已正确添加到列表中,并带有图标).但是显示性别的图标被添加到 ListView 中的错误项目中.名称被添加到列表的底部,但图标被放置在列表顶部的名称处.我不知道这是否是我使用 ViewHolder 的方式,但是 Android 网站.

//列表视图充气器inflater = (LayoutInflater) (this).getSystemService(LAYOUT_INFLATER_SERVICE);//列表数组.mAdapter = new ArrayAdapter(this, R.layout.player_simple_list,R.id.label, mStrings) {@覆盖public View getView(int position, View convertView, ViewGroup parent) {Log.i("ANDY","View getView Called");//一个 ViewHolder 保持对子视图的引用//避免在每一行上不必要地调用 findViewById().ViewHolder 支架;if (null == convertView) {Log.i("ANDY","位置以前没有使用过,所以膨胀了");convertView = inflater.inflate(R.layout.player_simple_list, null);//创建一个 ViewHolder 并存储对//我们想要绑定数据的两个子视图.持有人 = 新的 ViewHolder();holder.text = (TextView) convertView.findViewById(R.id.label);holder.icon = (ImageView) convertView.findViewById(R.id.icon);if (sexmale == true) {holder.icon.setImageBitmap(maleicon);}别的 {holder.icon.setImageBitmap(femaleicon);}convertView.setTag(holder);} 别的 {//取回 ViewHolder 以快速访问 TextView//和 ImageView.持有人 = (ViewHolder) convertView.getTag();}//有效地将数据与持有者绑定.holder.text.setText(getItem(position));//改变图标取决于sexmale 变量是真还是假.Log.i("ANDY","getCount = "+mAdapter.getCount());返回转换视图;}};setListAdapter(mAdapter);

解决方案

更新: ViewHolder 仅用于保存对项目布局内的组件视图的引用.这有助于避免调用 findViewById 以在具有多个组件的复杂项目布局中渲染每个组件的开销(例如 TextViewImageView 在此案例).

我通过使用例程(称为 getSex)来检索性数据并设置所有视图数据(包括 if-else 块之外的图标)来修复它.>

现在的工作代码如下所示:

if (null == convertView) {Log.i("ANDY","位置以前没有使用过,所以膨胀了");convertView = inflater.inflate(R.layout.player_simple_list, null);//创建一个 ViewHolder 并存储对两个子视图的引用//我们想要绑定数据.持有人 = 新的 ViewHolder();holder.text = (TextView) convertView.findViewById(R.id.label);holder.icon = (ImageView) convertView.findViewById(R.id.icon);convertView.setTag(holder);} 别的 {//取回 ViewHolder 以快速访问 TextView//和 ImageView.持有人 = (ViewHolder) convertView.getTag();}//有效地将数据与持有者绑定.holder.text.setText(getItem(position));//改变图标取决于sexmale 变量是真还是假.if (getSex (getItem(position)) == true) {holder.icon.setImageBitmap(maleicon);}别的 {holder.icon.setImageBitmap(femaleicon);}返回转换视图;

I have a dynamic ListView which uses an ArrayAdapter. When a name is selected from a spinner, the name together with an icon showing whether they are male or female gets added to the ListView.

Mostly everything is good (the name gets added to the list correctly, together with an icon). But the icon showing the sex gets added to the wrong item in the ListView. The name gets added to the bottom of the list, but the icon gets placed at the name at the top of the list. I don't know if it's the way I'm using ViewHolder but there is zero documentation on it in the 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);

解决方案

Update: ViewHolder is only meant to hold references to the component views inside the item layout. This helps to avoid the overhead of calling findViewById for rendering each component inside complex item layouts with multiple components(Like the TextView, and ImageView in this case).

I fixed it by using a routine (called getSex) to retrieve the sex data and setting all the view data including icons outside the if-else blocks.

The working code now looks like this:

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);
    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 (getSex (getItem(position)) == true)  {
    holder.icon.setImageBitmap(maleicon);
}
else {
    holder.icon.setImageBitmap(femaleicon);
}
return convertView;

这篇关于带有 ArrayAdapter 和 ViewHolder 的 ListView 将图标添加到错误的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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