ArrayAdapter ,检查复选框是否被选中并在动画后删除行
ArrayAdapter , check if checkbox is checked and remove the row after animation
您好,如何检查复选框是否被选中,以及是否被选中
我怎样才能删除有问题的行?
谢谢
数组适配器:
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 class 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
问题是该行仍然可见
这就是方法,我不知道你是如何填充你的列表的,如果你正在使用 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
}
您好,如何检查复选框是否被选中,以及是否被选中 我怎样才能删除有问题的行? 谢谢
数组适配器:
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 class 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
这就是方法,我不知道你是如何填充你的列表的,如果你正在使用 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
}