在“自定义适配器convertView”方法中“缓存”我的getView时遇到问题。当使用“缓存”之类的

if (convertView == null) ...


即使未验证用户,有时也会显示我的verifiedView。当删除if构造并仅使用row = convertView;时,一切都会按预期进行。任何想法如何解决这个问题?

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

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(resource, parent, false);
    } else {
        row = convertView;
    }

     UserProfile user = users.get(position);
    // get views
    TextView usernameView = (TextView) row.findViewById(R.id.username);
    ImageView avatarView = (ImageView) row.findViewById(R.id.avatar);
    ImageView verifiedView = (ImageView) row.findViewById(R.id.verified);

    // check if user has an custom avatar
    if (!user.getAvatar().isEmpty()) {
        // now check if we already downloaded the avatar
        InputStream is = AvatarHandler.getAvatar(this.context, user.getID());
        if (is != null) {
            Bitmap bmp = BitmapFactory.decodeStream(is);
            System.out.println("Avatar already downloaded.");
            avatarView.setImageBitmap(bmp);
        }
    }
    else {
        // set default avatar
        avatarView.setImageResource(R.drawable.avatar);
    }

    // set username
    usernameView.setText(user.getName());

    // set verified
    if (user.isVerified())
        verifiedView.setVisibility(View.VISIBLE);

    return row;
}

最佳答案

未验证用户时,应隐藏verifiedView

// set verified
if (user.isVerified()) {
        verifiedView.setVisibility(View.VISIBLE);
} else {
        verifiedView.setVisibility(View.GONE);
}

关于android - ArrayAdater getView“缓存” convertView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23520218/

10-11 02:57