Java – Programmatically setting the textview property within a card item causes random card modifications

Programmatically setting the textview property within a card item causes random card modifications… here is a solution to the problem.

Programmatically setting the textview property within a card item causes random card modifications

My goal is to programmatically change the text size of textview items in certain Cardviews that are contained in recyclerview. I succeeded in doing this, but with an unusual side effect. What then happens is that while the correct card View’s TextView property has been successfully modified, the random card that should not have been modified is now modified. However, as the user scrolls through more and more recyclerviews, more and more cards are modified in both directions. Eventually, starting with a few random cards (except my request card) caused all calls to be modified.

A better example: let’s say I have 5 cards and I want to change the text size to something else in a recyclerview with 50 cards. Now, those cards, as well as some other random cards, now have big text. The rest is still small. As the user scrolls up and down, more and more cards are set to large text until all cards have large text.

How my code works, I have a class, ListAdapter extension RecyclerView.Adapter<ListAdapter.VersionViewHolder> Because I collect my objects from the database, I use arraylists to collect my information and add them programmatically, As shown below:

@Override
public void onBindViewHolder(VersionViewHolder versionViewHolder, int i) {
    if (ActivitiesList.size() != 0)
        versionViewHolder.title.setText(ActivitiesList.get(i));
    if (ActivitiesSubList.size() != 0)
        versionViewHolder.subTitle.setText(ActivitiesSubList.get(i));
    if (ActivitiesSubList2.size() != 0)
        versionViewHolder.subTitle2.setText(ActivitiesSubList2.get(i));
    if (ActivitiesID.size() != 0)
        versionViewHolder.id.setText(ActivitiesID.get(i));

 TODO: POST QUESTION ON STACK OVERFLOW
    if (! ActivitiesTag.get(i).equals("")) {
        Log.d("CardPos", Integer.toString(versionViewHolder.getAdapterPosition()));
        versionViewHolder.title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 56);
    }

}

I set the ActivitiesList, ActivitiesSubList, ActivitiesSubList2, ActivitiesID, and ActivitiesTag in the subclass GradesListAdapter: This extends the ListAdapter

Note the list of ActivitiesTag arrays. When I wish to add a larger value, I simply insert any non-null value into the array list. Otherwise, I add an empty string. Therefore, to make it bigger, the criteria must be successfully met. (By the way, is there any way to improve this method?) In testing, it seems that only cards with the correct position are set to large, but it also affects unlabeled cards.

This is the VersionViewHolder class:

class VersionViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    CardView cardItemLayout;
    TextView title;
    TextView subTitle;
    TextView subTitle2;
    TextView id;

public VersionViewHolder(View itemView) {
        super(itemView);
        cardItemLayout = (CardView) itemView.findViewById(R.id.grades_item);
        title = (TextView) itemView.findViewById(R.id.listitem_name);
        subTitle = (TextView) itemView.findViewById(R.id.listitem_subname);
        subTitle2 = (TextView) itemView.findViewById(R.id.listitem_subname2);
        id = (TextView) itemView.findViewById(R.id.listitem_id);
        itemView.setOnClickListener(this);
    }

@Override
    public void onClick(View v) {
        clickListener.onItemClick(v, getAdapterPosition());
    }
}

What can I do to eliminate this unexpected side effect? I believe I have all the data someone else needs to help debug it, but if I’m missing something, say so; I’m still new to StackOverflow.

Thanks for reading.

Solution

In your onbindViewHolder, change

if (! ActivitiesTag.get(i).equals("")) {
    Log.d("CardPos", Integer.toString(versionViewHolder.getAdapterPosition()));
    versionViewHolder.title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 56);
}

to

if (! ActivitiesTag.get(i).equals("")) {
    Log.d("CardPos", Integer.toString(versionViewHolder.getAdapterPosition()));
    versionViewHolder.title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 56);
}else{
    versionViewHolder.title.setTextSize(TypedValue.COMPLEX_UNIT_SP,"Your Original Text Size");
}

If you only set an if clause in the viewholder without setting the else clause, your text size will not be reset to normal because the view is reused again and again. Therefore, you have to reset the text size manually.

Related Problems and Solutions