Search code examples
androidlistviewcheckboxcustom-adapter

Hide or show CheckBox in CustomAdapter


I'm using databse in my app. I can create some "notes" , and define the type : Homework or Test.

Then I display it in a ListView. But, when the row is created, I'd like to check if I chose "Homework" as the type, and if I did, I add a checkbox to that row in my listView, but If I chose "Test", I do not display the CheckBox.

Then, with the checkbox I can set the homework as DONE or NOT DONE with the cb.isChecked(). But first I have to only display the checkBox if it's a homework. But I don't know how to check it..

How can I do that ?

Here's my CustomAdapter class

public class CustomAdapterJalons extends SimpleCursorAdapter {

private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;

public CustomAdapterJalons(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.layout = layout;
    this.mContext = context;
    this.inflater = LayoutInflater.from(context);
    this.cr = c;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return inflater.inflate(layout, null);
}

@Override
public void bindView(View view, final Context context, final Cursor cursor) {
    super.bindView(view, context, cursor);

    LinearLayout root_view = (LinearLayout)view.findViewById(R.id.item_root);

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

            //method
        }
    });

   final int position = cursor.getPosition();

    cursor.moveToPosition(position);

    int devoir = cursor.getInt(3);
    int evaluation = cursor.getInt(4);
    CheckBox cbdevoir = (CheckBox)view.findViewById(R.id.CBDevoir);


    if(devoir==1)
    {
     cbdevoir.setVisibility(View.VISIBLE);
    }
   if(evaluation==1)
   {
       cbdevoir.setVisibility(View.INVISIBLE);
   }
    }

}

Thank you guys !


Solution

  • you have to check that in bindview method

    @Override
    public void bindView(View view, final Context context, final Cursor cursor) {
        super.bindView(view, context, cursor);
    
        LinearLayout root_view = (LinearLayout)view.findViewById(R.id.item_root);
    
        root_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                //method
            }
        });
    
       final int position = cursor.getPosition();
    
        cursor.moveToPosition(position);
    
        int devoir = cursor.getInt(3);
        int evaluation = cursor.getInt(4);
        CheckBox cbdevoir = (CheckBox)view.findViewById(R.id.CBDevoir);
    
    
        if(devoir==1)
        {
         cbdevoir.setVisibility(View.VISIBLE);
        }
        else{
        cbdevoir.setVisibility(View.INVISIBLE);
        }
    
        }