2

我的所有,我有一个问题,不允许我在 ListView 中的一行显示图标,只有当该行的一个数据与来自另一个来源的参数匹配时。在我的示例中,我将强制此参数为静态。

我有这个基本上运行良好的代码:

public class CustomListViewAdapter extends ArrayAdapter<User> {

Context context;
List<User> items;

public CustomListViewAdapter(Context context, int resourceId, List<User> items) {
    super(context, resourceId, items);
    this.context = context;
    this.items = items;
}

public int getCount() {
    return this.items.size();
}

/* private view holder class */
private class ViewHolder {
    ImageView rubrPhoto;
    TextView rubrNick;
    TextView rubrEmail;
    ImageView provaImg;
}

public View getView(int position, View convertView, ViewGroup parent) {
    Log.d("CustomListViewAdapter", "position = "+position);
    ViewHolder holder = null;
    User user = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.rubrica_list_view, null);

        holder = new ViewHolder();
        holder.rubrPhoto = (ImageView) convertView.findViewById(R.id.rubrPhoto);
        holder.rubrNick = (TextView) convertView.findViewById(R.id.rubrNick);
        holder.rubrEmail = (TextView) convertView.findViewById(R.id.rubrEmail);
        holder.provaImg = (ImageView) convertView.findViewById(R.id.rubrNewMess);

        //extra logic
        String email = user.getEmail();
        Log.d("CustomListViewAdapter", "FIRST TIME CREATED---> "+email);
        if(email.equals("giada@email.com")){
            Log.d("CustomListViewAdapter", "##############IN THE IF ... ");
            holder.provaImg.setVisibility(View.VISIBLE);
        }
        //end

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();

        //extra logic
        Log.d("CustomListViewAdapter", "CACHED---> email user ---->"+user.getEmail());
        CharSequence email = user.getEmail();

        if(email.equals("giada@email.com")){
            Log.d("CustomListViewAdapter", "IN THE IF.. ");
            holder.provaImg.setVisibility(View.VISIBLE);
        }
    //end
    }
    holder.rubrPhoto.setImageResource(user.getPhoto());
    holder.rubrNick.setText(user.getNick());
    holder.rubrEmail.setText(user.getEmail());
    holder.provaImg.setImageResource(user.getNewMess());

    return convertView;
}

最初,所有 holder.provaImg 都通过 XML 布局将 VISIBILITY 设置为 GONE。但是,适配器的缓存机制似乎不能很好地工作,至少从我必须做的事情来看。

编码

User user = getItem (position);

String email = user.getEmail ();

是正确的并且与我所看到的相符。最初只显示正确的图标,但是当我开始滚动某些东西时崩溃并且 getView 将图标的可见性设置在错误的行中,好像缓存机制的工作方式与逻辑不同。

最后getView()设置电子邮件不匹配的行中图标的可见性。

也许我接近解决方案,但帮助肯定会挽救我的生命。非常感谢你。

4

1 回答 1

2

您错误地设置了provaImg ImageView的可见性。它应该是这样的:

if (convertView == null) {
//...
    convertView.setTag(holder);
}else{
    holder = (ViewHolder) convertView.getTag();
}
holder.rubrPhoto.setImageResource(user.getPhoto());
holder.rubrNick.setText(user.getNick());
holder.rubrEmail.setText(user.getEmail());
holder.provaImg.setImageResource(user.getNewMess());
CharSequence email = user.getEmail();
// if the email is the right one, show the ImageView
if(email.equals("giada@email.com")){
    Log.d("CustomListViewAdapter", "##############IN THE IF ... ");
    holder.provaImg.setVisibility(View.VISIBLE);
} else {
    // hide the ImageView so we don't end up with row problems
    // as the convertView could be non-null and the row from where that convertView
    // is coming could have the ImageView set to visible we need to make sure
    // we revert the row to the correct state
    holder.provaImg.setVisibility(View.INVISIBLE); // or GONE
}
于 2013-01-03T18:18:30.623 回答