-1

I use this to visible some textview

 public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Post p = posts.get(position);
        holder.textView1.setText(p.getName());
        holder.textView2.setText(p.getFamilyName());
        if (posts.get(position).getBoolean()){
            holder.textView3.setVisibility(View.VISIBLE);
        }
        
    }

but when I use notifyDataSetChange(); I have some problem what is the correct way to set the if(){} in recycler view?

2 Answers2

0

Try this way:

public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Post p = posts . get (position);
    holder.textView1.setText(p.getName());
    holder.textView2.setText(p.getFamilyName());
    holder.textView3.setVisibility(
        if (posts.get(position).getBoolean()) {
            View.VISIBLE
        } else {
            View.GONE
        }
    );
}
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
JimenezR
  • 11
  • 1
  • This would be a better answer if you explained how the code you provided answers the question. – pppery May 02 '22 at 01:43
0

You missed the else part .

  public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Post p = posts.get(position);
    holder.textView1.setText(p.getName());
    holder.textView2.setText(p.getFamilyName());
    holder.textView3.setVisibility(posts.get(position).getBoolean() ? View.VISIBLE : View.GONE);
}
ADM
  • 20,406
  • 11
  • 52
  • 83