您好,我如何检查复选框是否已选中,以及是否已选中 我怎样才能删除有问题的行? 谢谢你
数组适配器:
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;
}
}
我的主要 Activity :
公共(public)类 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
最佳答案
这就是方法,我不知道您如何填充列表以及是否使用回收器 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
}
关于java - ArrayAdapter ,检查复选框是否被选中并在动画后删除该行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48131901/