Android 在客户适配器内设置映像(非活动)

Android 在客户适配器内设置映像(非活动),android,imageview,Android,Imageview,我在另一个Imageview上有一个最喜欢的ImageButton。这是正确的,但当我设置新的形象,相应地在我得到两个相同的形象 我不确定这哪里错了 以下是我的XML(具有ImageView): 使用上述代码。当我点击喜爱的图标时,我可以同时看到两个图标。一个旧图像和另一个新图像 如何清除旧图像 塞纳克斯 您不需要执行ImageButton favi=(ImageButton)v 该imageButton应已存储在viewHolder.Favorite中。这是因为您的视图是循环使用的,您没有在i

我在另一个Imageview上有一个最喜欢的ImageButton。这是正确的,但当我设置新的形象,相应地在我得到两个相同的形象

我不确定这哪里错了

以下是我的XML(具有ImageView):

使用上述代码。当我点击喜爱的图标时,我可以同时看到两个图标。一个旧图像和另一个新图像

如何清除旧图像


塞纳克斯

您不需要执行
ImageButton favi=(ImageButton)v


该imageButton应已存储在viewHolder.Favorite中。

这是因为您的视图是循环使用的,您没有在imageButton的视图上设置任何标记,并且在imageButton p.s的onClick()内没有任何标记。请在此处检查我的答案:。我认为问题是相似的,解决了。非常感谢Fondesa。虽然链接没有给我太多的用处,但你的逻辑给了我。
   <ImageView
    android:id="@+id/item_image"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_below="@+id/grid_regular_image"
    android:layout_centerHorizontal="true"
    android:src="@drawable/none"
    tools:ignore="ContentDescription" />

<ImageButton
    android:id="@+id/favouriteIcon"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/item_image"
    android:background="@android:color/transparent"
    android:paddingBottom="20dp"
    android:paddingRight="5dp"
    android:visibility="invisible" />
@Override
public View getView(int position, View convertView, final ViewGroup parent) 
{
    ViewHolder holder = null;

    if (convertView == null)
    {
        convertView = vi.inflate(R.layout.emb_gridlist_items, null);

        holder = new ViewHolder();
        holder.favourite = (ImageButton) convertView.findViewById(R.id.favouriteIcon);
        holder.favourite.setVisibility(View.VISIBLE);

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

    favouriteclicked = pref.getString("favourite", getContext().getApplicationContext().getResources().getString(R.string.favouritenotclicked));

    if (favouriteclicked.equalsIgnoreCase(getContext().getApplicationContext().getResources().getString(R.string.favouritenotclicked)))
    {
        holder.favourite.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_alpha));
        holder.favourite.setImageResource(R.drawable.ic_starfill);

        SharedPreferences.Editor editor6 = pref.edit();
        editor6.putString("favouriteclicked", getContext().getApplicationContext().getResources().getString(R.string.favouriteclicked));
        editor6.commit();
    }
    else
    {
        holder.favourite.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_alpha));

        holder.favourite.setImageResource(R.drawable.ic_stillnofill);

        SharedPreferences.Editor editor6 = pref.edit();
        editor6.putString("favouriteclicked", getContext().getApplicationContext().getResources().getString(R.string.favouritenotclicked));
        editor6.commit();
    }

    holder.favourite.setOnClickListener( new View.OnClickListener() 
    {
        public void onClick(View v)  
        {
            favouriteclicked = pref.getString("favourite",    getContext().getApplicationContext().getResources().getString(R.string.favouriteclicked));

            ImageButton favi = (ImageButton) v;

            if (favouriteclicked.equalsIgnoreCase(getContext().getApplicationContext().getResources().getString(R.string.favouritenotclicked)))
            {
                favi.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_alpha));

                favi.setBackgroundResource(R.drawable.ic_starfill);

                SharedPreferences.Editor editor6 = pref.edit();
                editor6.putString("favouriteclicked", getContext().getApplicationContext().getResources().getString(R.string.favouriteclicked));
                editor6.commit();
            }
            else
            {
                favi.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_alpha));

                favi.setBackgroundResource(R.drawable.ic_stillnofill);

                SharedPreferences.Editor editor6 = pref.edit();
                editor6.putString("favouriteclicked", getContext().getApplicationContext().getResources().getString(R.string.favouritenotclicked));
                editor6.commit();
            }
        }
    });
    }