Hello how can I check if a checkbox is check, and if it is check how can I remove the row in question ? thank you
ArrayAdapter :
public class Todoadapter extends ArrayAdapter<Todo> {
private Context mcontext;
int mresource;
public Todoadapter(@NonNull Context context, int resource, @NonNull List<Todo> objects) {
super(context, resource, objects);
this.mresource=resource;
this.mcontext= context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
String todo = getItem(position).getTodo();
LayoutInflater inflater = LayoutInflater.from(mcontext);
convertView = inflater.inflate(mresource,parent,false);
CheckBox box = (CheckBox)convertView.findViewById(R.id.checkBox2);
box.setText(todo);
return convertView;
}
}
My main activity :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView view = (ListView)findViewById(R.id.listview);
Todo eat = new Todo("Eat ");
Todo sleep = new Todo("Sleep");
ArrayList<Todo> todolist = new ArrayList<>();
todolist.add(eat);
todolist.add(sleep);
Todoadapter adapter = new Todoadapter(this,R.layout.custom_adapter_layout,todolist);
view.setAdapter(adapter);
}
}
My XML
This is the approach, I don't know exactly how you are populating your list and if you are using the Recycler View, but this should be the body for what you want.
box.setText(todo);
box.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(box.isChecked()){
//Delay to see animation
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
objects.remove(getItem(position)); //Your object list where you have the itens
notifyDataSetChanged(); //If you are using a recycler view.
}
},300); //adding 0.3 sec delay
}
}
});
return convertView;
EDIT: For get your list, probably you should do something like this:
private List<Todo> objects = new List<>(); //NEW
private Context mcontext;
int mresource;
public Todoadapter(@NonNull Context context, int resource, @NonNull List<Todo> objects) {
super(context, resource, objects);
this.mresource=resource;
this.mcontext= context;
this.objects = objects //NEW
}