David
David

Reputation: 221

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 !

Upvotes: 0

Views: 148

Answers (3)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

Your condition is not OK. In your adapters bindView() method:

Use:

if(devoir == 1) {
    cbdevoir.setVisibility(View.VISIBLE);
} else {
    cbdevoir.setVisibility(View.GONE);
}

Instead of:

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

Upvotes: 1

Pavan
Pavan

Reputation: 5136

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

    }

Upvotes: 1

Rajasekher reddy
Rajasekher reddy

Reputation: 383

Steps

  1. In XML row make checkbox is hide

android:visibility="gone"

  1. 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
        }
    });
    
    
    CheckBox cbdevoir = (CheckBox)view.findViewById(R.id.CBDevoir);
    
    If(type.equals("Homework"){
    
      cbdevoir.setVisibility(View.VISIBLE)
     }
    else{
    
        cbdevoir.setVisibility(View.GONE);
    }
    
    
    if(cbdevoir.isChecked())
    {
        //method to set the homework done
    }
    else
        {
            //method to set the homework undone
        }
    }
    

Upvotes: 0

Related Questions