Java – Update specific projects in recyclerview

Update specific projects in recyclerview… here is a solution to the problem.

Update specific projects in recyclerview

I have a RecyclerView and adapter

public class AdapterToUpdateOk extends RecyclerView.Adapter<AdapterToUpdateOk.VersionViewHolder> {
String[] desc;
Context context;
LayoutInflater inflater;

AdapterToUpdateOk adapter;

public AdapterToUpdateOk(Context context,String [] desc) {
    this.context = context;
    this.desc= desc;
    inflater = LayoutInflater.from(context);
}

@Override
public VersionViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = inflater.inflate(R.layout.inflater_sugessted_friends, viewGroup, false);
    return new VersionViewHolder(view);
}

@Override
public void onBindViewHolder(final VersionViewHolder versionViewHolder, final int i) {
    final VersionViewHolder v = versionViewHolder;
    versionViewHolder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            versionViewHolder.imageView.setImageResource(R.drawable.ic_ok);
        }
    });

}

@Override
public int getItemCount() {
    return desc.length;
}

class VersionViewHolder extends RecyclerView.ViewHolder {
    Button button;
    ImageView imageView;;
    public VersionViewHolder(final View itemView) {
        super(itemView);
        button= itemView.findViewById(R.id.button);
        imageView = itemView.findViewById(R.id.imageview);
    }

}

Currently it works, but it affects other parts that are not clicked

Solution

Answer: (Update a specific item in the Recycle Bin View).

notifyItemChanged (Adapterposition), working fine, here are some descriptions

The notifyItemChanged method tells OnBind to redraw the item at that location.
Before calling this method, you must consider updating the arrayData at that location to reflect the new changes

      versionViewHolder.ClickedView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ArrayDataAt[position] = " New Data to replace Old one ";
            notifyItemChanged(Adapterposition);
        }
    });

What if you want to change something that is not from an array dataset?

This is exactly the same as in my case, (and same as updating background color, text color, etc.)

You can create an identifier (just a boolean array) that contains the state of your rows, assuming that the boolean values of all rows are false, when clicking Update booleanArray to true at that location, remember to tell bind to consider true elements different colors, different fonts and other specific operations, and finally notify notifyItemChanged(Adapterposition);

So what’s wrong with my code

  1. The Onclick event changes, but after scrolling away from the View and back again, OnBind continues to recreate the View with the same data as the old View. (It is important to update the array data of position or have a flag that shows that the data has been updated).

  2. My View has drawable items, and I never update it when bind, so when clicked, items that appear to be in the same UI as the clicked item also reflect the change. (Mixes the concepts of dynamic View elements and static View elements).

What if someone’s data comes from SQLite?

Consider putting the data into an array first.

Onclick updates your SQLite data in the background and updates the View to reflect that data (don’t waste memory redrawing the entire recyclerview from SQLite again).

Thanks to all contributors.

Related Problems and Solutions