【发布时间】:2017-05-22 12:32:10
【问题描述】:
我的行中有一个Listview,我有一个复选框来选择要删除的项目。
我想要一个按钮来选择我的所有项目并同时删除它们。
当我单击按钮选择所有项目时,只有最后一个项目被选中。
这是我的代码:
all.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
for(int i=0;i<listview.getChildCount();i++){
Log.i("mhs",listview.getChildCount()+"");
checkBox.setChecked(true);
}
}
});
这是我的Adaptor:
public class CustomArrayAdapter extends ArrayAdapter<String> {
private LayoutInflater inflater;
// This would hold the database objects i.e. Blacklist
private List<Blacklist> records;
@SuppressWarnings("unchecked")
public CustomArrayAdapter(Context context, int resource, @SuppressWarnings("rawtypes") List objects) {
super(context, resource, objects);
this.records = objects;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//Reuse the view to make the scroll effect smooth
if (convertView == null)
convertView = inflater.inflate(R.layout.list_item, parent, false);
// Fetch phone number from the database object
final Blacklist phoneNumber = records.get(position);
// Set to screen component to display results
((TextView) convertView.findViewById(R.id.serial_tv)).setText("" + (position + 1));
((TextView) convertView.findViewById(R.id.phone_number_tv)).setText(phoneNumber.phoneNumber);
checkBox = (CheckBox) convertView.findViewById(R.id.check);
final LinearLayout me = (LinearLayout) convertView.findViewById(R.id.me);
if (position + 1 >= 1) {
me.setVisibility(View.VISIBLE);
}
checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
// Set the appropriate message into it.
alertDialogBuilder.setMessage(getResources().getString(R.string.block));
// Add a positive button and it's action. In our case action would be deletion of the data
alertDialogBuilder.setPositiveButton(getResources().getString(R.string.yes),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
try {
blackListDao.delete(blockList.get(position));
// Removing the same from the List to remove from display as well
blockList.remove(position);
listview.invalidateViews();
// Reset the value of selectedRecordPosition
selectedRecordPosition = -1;
populateNoRecordMsg();
} catch (Exception e) {
e.printStackTrace();
}
}
});
// Add a negative button and it's action. In our case, just hide the dialog box
alertDialogBuilder.setNegativeButton(getResources().getString(R.string.no),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
checkBox.setChecked(false);
}
});
// Now, create the Dialog and show it.
final AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
all.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
for(int i=0;i<listview.getChildCount();i++){
Log.i("mhs",listview.getChildCount()+"");
checkBox.setChecked(true);
listview.removeView(i);
}
}
});
return convertView;
}
}
如何同时选择所有项目?
【问题讨论】:
-
只需像 YOUR_ARRAYLIST.clear() 一样清除您的列表;并确保在清除列表后使用notifyDatachange
-
也发布你的适配器类
-
我假设您想从某种持久性系统数据库、文件中删除项目...您只需删除所有这些项目,然后更新您的适配器
-
我刚刚选择的最后一项。现在我什么都不清除。首先我必须让他们聚在一起,然后删除他们
-
配合什么是“复选框”?您正在执行 for 循环,但没有使用增量来获取当前项目。