我正在尝试从名为arrayofTask的数组列表中删除一项 通过选中一个复选框。我尝试了多种方法来实现此目的,但未成功。
访问https://imgur.com/gallery/oRdhpGR,这是我上传列表视图的图片的地方,我使用添加按钮添加了任务和说明。
我想做的是通过单击复选框删除此类任务和说明
贝洛是我一直想做的事,但我不知道该怎么做。
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox.isChecked()){
///i want to delete the arrayoftasks thats located in this location.
///tho i am unable to get the location for the tasks in this location.
///so if i check the box it should delete the items
}
}
});
这是我的自定义适配器
public class userAdapter extends ArrayAdapter<user> {
public userAdapter(Context context, ArrayList<user> users){
super(context,0,users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
final user User = getItem(position);
CheckBox checkbox;
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
}
///this new
checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
///above
TextView tvTask = (TextView) convertView.findViewById(R.id.task);
TextView tvDescription = (TextView) convertView.findViewById(R.id.description);
tvTask.setText(User.task);
tvDescription.setText(User.description);
return convertView;
}
答案 0 :(得分:0)
我无法打开您的图片,但是此代码是为回收站视图编写的,但也适用于列表视图,当单击项目的每个复选框时,项目将从阵列列表和回收站视图中删除。
注意数组列表。
public void onBindViewHolder(ViewHolder holder, final int position) {
//hodler
holder.txtName.setText(dataObjects.get(position));
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(CheckActivity.context, "" + position, Toast.LENGTH_SHORT).show();
CheckActivity.arrayList.remove(position);
CheckActivity.adapter.notifyDataSetChanged();
}
}
});
}