Search code examples
androidandroid-recyclerviewnotifydatasetchanged

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

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