简体   繁体   中英

Setter in RecyclerViewAdapter not working?

I have a boolean in my onBindViewHolder which decides whether a button is clickable or not. In the constructor I set this value to false, later when the user unlocks something I want to set it to true with a setter.

Here is my RecyclerViewAdapter.java:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    private ArrayList<Items> items;
    Context mContext;
    private boolean unlocked;

    public RecyclerViewAdapter(Context context, ArrayList<Items> items, boolean unlocked) {
        mContext = context;
        this.items = items;
        this.unlocked = tabUnlocked;
    }

    public void setUnlocked(boolean unlocked) {
        this.unlocked = unlocked;
        this.notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerViewAdapter.iewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        v.setLayoutParams(lp);
        return new RecyclerViewAdapter.ViewHolder(v);
    }




    @Override
    public void onBindViewHolder(@NonNull final RecyclerViewAdapter.ViewHolder holder, int position) {
        final Items currentItem = items.get(position);

        final String name = currentItem.getName();
        holder.itemTextView.setText(name);

  
        holder.itemTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (unlocked) {
                     //do something
                }
            }
        }); 
    }

 

    @Override
    public int getItemCount() {
        return item.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView itemTextView; 

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemTextView = itemView.findViewById(R.id.textViewItem); 
        }
    }



}

And here is how I call the Setter to change it: I make the RecyclerView public:

public RecyclerViewAdapter adapter;

Then I create an instance in my onCreate:

recyclerView = rootView.findViewById(R.id.recyclerView); 
adapter = new RecyclerViewAdapter(items);
recyclerView.setAdapter(adapter);

And when the user unlocks a certain thing I call this method of the current Class:

public void unlockTab() {
 
    adapter.setUnlocked(true); 

}

When I log the boolean inside of that setter it tells me it got changed. But when I log the boolean inside the onBindViewHolder it still remains false.

Why is the setter method not setting the boolean to true in the whole RecyclerViewAdapter.java class?

Hope someone can help me!

EDIT I already tried adding "this.notifyDataSetChanged();" to the Setter Method (Thanks to @a_local_nobody)

Adapters are responsible for changes to recyclerviews. but if you don't tell them something changed, they won't know to do so.

call notifyDataSetChanged();

public void unlockTab() {
 
    adapter.setUnlocked(true); 
    adapter.notifyDataSetChanged();

}

what does it do?

It tells your recyclerview to bind all the data again, so then your changes will apply

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM