3

I have an adapter with the ViewHolder pattern. It has a TextView and an ImageView. I have to do some check with database in the adapter to show the ImageView or not. The problem is that is set the visibility to View.VISIBLE also to other ImageView in other positions. I'm sure that my database doesn't contain those data related to those imageview, but only to the one that is correctly set.

Here is my code (I deleted the part of the textview's set because it worked well):

public class ChaptersAdapter extends ArrayAdapter<List> {
    private final Context context;
    private final ArrayList<List> list;
    ViewHolder viewHolder;
    DatabaseHandler dh;
    SQLiteDatabase db;
    ArrayList<BookmarksHandler> bookmarksAL;

    public ChaptersAdapter(Context context, ArrayList<List> list) {
        super(context, R.layout.chaptersadapter, list);

        this.context = context;
        this.list = list;
    }

    static class ViewHolder{
        TextView titolo;
        ImageView imageView;
    }

    @Override
    public View getView(int position, View rowView, ViewGroup parent) {
        dh = new DatabaseHandler(context);
        db = dh.getWritableDatabase();
        bookmarksAL = dh.getAllBookmarks(db);

        List chapters = getItem(position);

        if(rowView==null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.chaptersadapter, parent, false);

            viewHolder = new ViewHolder();

            viewHolder.titolo = (TextView) rowView.findViewById(R.id.textView);
            viewHolder.imageView = (ImageView) rowView.findViewById(R.id.imageView);

            rowView.setTag(viewHolder);
        }
        else {
            viewHolder = (ViewHolder) rowView.getTag();
        }

        for(int i = 0; i<bookmarksAL.size(); i++){
            if(bookmarksAL.get(i).getId_chapter().equals((String)chapters.get(3))){
                viewHolder.imageView.setVisibility(View.VISIBLE);
            }
        }


        return rowView;
    }
}

And here is a screen of what i mean: screen viewholder

Only the imageview with circle has to be added, not the one with X above. If i scroll down the listview, the viewholder set the visibility to visible also to other imageviews that aren't in my database.

Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70

2 Answers2

4

it is the normal behavior of the ListView, due to its recycling mechanism. Reset the ImageView's visibility if the condition is not met

for(int i = 0; i<bookmarksAL.size(); i++){            
  if(bookmarksAL.get(i).getId_chapter().equals((String)chapters.get(3)))  {            
      viewHolder.imageView.setVisibility(View.VISIBLE);
      break;
  } else {
      viewHolder.imageView.setVisibility(View.GONE);
  }
}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

For other items that are not visible you also need set like:

  for(int i = 0; i<bookmarksAL.size(); i++){
        if(bookmarksAL.get(i).getId_chapter().equals((String)chapters.get(3))){
            viewHolder.imageView.setVisibility(View.VISIBLE);
        }
        else
            viewHolder.imageView.setVisibility(View.INVISIBLE);
    }

It is better to set INVISIBLE because GONE will change your layout.

Hope this help!

Xcihnegn
  • 11,579
  • 10
  • 33
  • 33