您好我如何检查复选框是否已检查,以及是否检查 如何删除有问题的行? 谢谢
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;
}
}
我的主要活动:
公共类MainActivity扩展了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);
}
}
我的XML
答案 0 :(得分:1)
这是方法,我不确切知道你如何填充你的清单,如果你正在使用Recycler View,但这应该是你想要的身体。
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;
编辑: 为了得到你的清单,你可能应该这样做:
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
}