'how to set condition in adapter?

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?



Solution 1:[1]

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);
}

Solution 2:[2]

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
        }
    );
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ADM
Solution 2 Sunderam Dubey