Ava
Ava

Reputation: 522

button.OnClick code runs but doesn't execute changes?

I'm trying to make an OnClick listener for an imagebutton, that is supposed to remove a bitmap (set it to null) on click. However, nothing happens when I press the button, except it does print my "Log.d()" message, and the bitmap IS actually null after the click, however changes doesn't show. The code is written inside my adapter. I even tried inserting it inside a runOnUIThread to see if that makes a difference, however there's no change. I also tried setImageDrawable(null) and setImageResource(0) with no luck. I even tried changing other UI elements, and that doesn't work either. Only thing I can make it do is print the log message...

Here's my getView with my onClick method:

    @Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    if (convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_create_product_gridview_normal, parent, false);
        //Set up viewholder
        viewHolder = new ViewHolder();
        viewHolder.productImageIv = (ImageView) convertView.findViewById(R.id.android_gridview_image);
        viewHolder.addImageIb = (ImageButton) convertView.findViewById(R.id.addImageButton);
        viewHolder.removeImageIb = (ImageButton) convertView.findViewById(R.id.removeImageButton);
        viewHolder.productImageIv.setImageBitmap(images[position]);
        checkForNullImages(viewHolder, position);
        //Store the holder with the view
        convertView.setTag(viewHolder);

        viewHolder.removeImageIb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                runOnUiThread (new Thread(new Runnable() {
                    public void run() {
                        images[position] = null;
                        viewHolder.productImageIv.setImageBitmap(images[position]);
                        notifyDataSetChanged();
                        //Does print this message, item is null, but image is still shown
                        Log.e("LogMSG", "item should be null " + getItem(position));
                    }
                }));

            }
        });
    }
    return convertView;
}

And if relevant, here's my corresponding XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/android_custom_gridview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"
    app:cardCornerRadius="10dp"
    app:cardBackgroundColor="@color/white">

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        app:cardCornerRadius="10dp">

        <ImageView
            android:id="@+id/android_gridview_image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/gradient_background"
            android:scaleType="centerCrop" />

    </android.support.v7.widget.CardView>

</android.support.v7.widget.CardView>

<ImageButton
    android:id="@+id/addImageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="-30dp"
    android:elevation="@dimen/standard_12"
    android:background="@drawable/icon_add" />

<ImageButton
    android:id="@+id/removeImageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="-30dp"
    android:background="@drawable/icon_remove"
    android:elevation="@dimen/standard_12" />

Upvotes: 0

Views: 49

Answers (1)

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29794

This is because your row view recycling is incorrect. You're not recycling the view with the following code:

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    if (convertView == null)
    {
      // ...
    }

    return convertView;
}

because you're not using the previous convertView if it's not null.

So, change your code to something like this:

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    ViewHolder holder;
    if (convertView == null) {
       LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_create_product_gridview_normal, parent, false);
        //Set up viewholder
        holder = new ViewHolder();
        holder.productImageIv = (ImageView) convertView.findViewById(R.id.android_gridview_image);
        holder.addImageIb = (ImageButton) convertView.findViewById(R.id.addImageButton);
        holder.removeImageIb = (ImageButton) convertView.findViewById(R.id.removeImageButton);

        // set click listener here.
        // don't use runOnUiThread for click listener.

        // bind the view here.

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

    // set the value for view here
    holder.productImageIv.setImageBitmap(images[position]);


    return convertView;
}

Upvotes: 1

Related Questions