在自定义适配器内设置图像(在活动中)

时间:2015-03-22 20:34:52

标签: android imageview

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

我不确定这是错的?

这是我的XML(具有ImageView):

   <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" />

这是我的自定义Apadter文件:这里我也在点击部分调用图像。

@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();
            }
        }
    });
    }

使用上面的代码。当我点击收藏夹图标时,我可以同时看到两个图标。一个旧图像和其他新图像。

如何清除旧图像?

Thnaks!

1 个答案:

答案 0 :(得分:0)

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

该imageButton应该已经存储在viewHolder.favourite中。