Search code examples
androidandroid-gridview

Android - Activated state remains true after deletion of item from GridView


I have a GridView that contains some cards. When I tap on a card it gets highlighted(a red border appears). I have a button that ,when clicked, it removes the highlighted cards. However, the red border remains on the next card.

What happens

What I want to happen

The selector that the LinearLayout of the card uses.

border_image_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.example.peridis.setfinder">
    <item android:state_activated="true" app:state_lighted="true"
       android:drawable="@drawable/border_image_lighted_activated"></item>
    <item app:state_lighted="true" android:drawable="@drawable/border_image_lighted" ></item>
    <item android:state_activated="true" android:drawable="@drawable/border_image_activated" ></item>
    <item android:drawable="@drawable/border_image" ></item>
</selector>

CardsAdapter.java

public class CardsAdapter extends ArrayAdapter {

private Context context;
private ArrayList<Card> cards;
public static final int ONE_SHAPE = 0;
public static final int TWO_SHAPES = 1;
public static final int THREE_SHAPES = 2;

public CardsAdapter(@NonNull Context context,int resource, @NonNull ArrayList<Card> cardsArryaList) {
    super(context, resource, cardsArryaList);
    this.context=context;
    cards = cardsArryaList;
}

@Override
public int getViewTypeCount() {
    return 3;
}

@Override
public int getItemViewType(int position) {
    return cards.get(position).getNumber() - 1;
}

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

    ViewHolder holder;
    Card card = cards.get(position);
    int cardNumber = getItemViewType(position);
    LayoutInflater inflater = LayoutInflater.from(context);
    int cardImageId = cards.get(position).getImageId(context);

    if(convertView == null)
    {
        if(cardNumber == ONE_SHAPE)
        {
            convertView = inflater.inflate(R.layout.card_one_shape_layout, parent, false);
        }
        else if(cardNumber == TWO_SHAPES)
        {
            convertView = inflater.inflate(R.layout.card_two_shapes_layout, parent, false);
        }
        else//cardNumber == THREE_SHAPES
        {
            convertView = inflater.inflate(R.layout.card_three_shapes_layout, parent, false);
        }

        ImageView cardImage1 = (ImageView) convertView.findViewById(R.id.cardImage1);
        ImageView cardImage2 = (ImageView) convertView.findViewById(R.id.cardImage2);
        ImageView cardImage3 = (ImageView) convertView.findViewById(R.id.cardImage3);

        holder = new ViewHolder();
        holder.image1 = cardImage1;
        holder.image2 = cardImage2;
        holder.image3 = cardImage3;

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


    holder.image1.setImageResource(cardImageId);



    if(holder.image2 != null)
    {
        holder.image2.setImageResource(cardImageId);
    }
    if(holder.image3 != null)
    {
        holder.image3.setImageResource(cardImageId);
    }

    return convertView;
}

static class ViewHolder
{
    ImageView image1;
    ImageView image2;
    ImageView image3;
}
}

The part of the main Activity that I use for deleting highlighted cards.

MainActivity.java

public class MainActivity extends AppCompatActivity {

ArrayList<Card> cards = new ArrayList<Card>();

GridView cardsContainerGridView;
CardsAdapter cardsAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cardsContainerGridView = (GridView) findViewById(R.id.cardsGridContainer);

    cardsAdapter = new CardsAdapter(this, R.id.cardImage1, cards);
    cardsContainerGridView.setAdapter(cardsAdapter);
}

public void removeButtonClicked(View v)
{
    for(int i=cardsContainerGridView.getChildCount()-1; i>=0; i--)
    {
        if(cardsContainerGridView.getChildAt(i).isActivated())
        {
            cards.remove(i);
        }
    }
    cardsAdapter.notifyDataSetChanged();
}

I tried setting ActivatedState to false after deletion but it didn't work.


Solution

  • Finally, I found out that I can use clearChoices on my GridView.

    public void removeButtonClicked(View v)
    {
        for(int i=cardsContainerGridView.getChildCount()-1; i>=0; i--)
        {
            if(cardsContainerGridView.getChildAt(i).isActivated())
            {
                cards.remove(i);
            }
        }
        cardsContainerGridView.clearChoices();
    
        cardsAdapter.notifyDataSetChanged();
    }