我有一个ListView
与CheckBox
. 我想从ListView
我们选择多个文件的示例 MyFiles 应用程序中获取所选项目,CheckBox
然后单击单个删除按钮以删除所有文件。
问问题
4691 次
4 回答
3
boolean bulkflag = false;
ListView reportslistview = (ListView) findViewById(android.R.id.list);
public class MyAdapter extends SimpleAdapter {
//private List<Table> tables;
SharedPreferences prefs;
private Activity activity;
String val = "";
//@SuppressWarnings("unchecked")
public MyAdapter(Activity context, List<? extends Map<String, String>> tables, int resource, String[] from,
int[] to) {
super(context, tables, resource, from, to);
//this.tables = (List<Table>) tables;
activity = context;
}
public View getView(final int position, final View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
if (row == null)
{
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.reports_list, null);
}
final CheckBox cBox=(CheckBox)row.findViewById(R.id.cb1);
if(bulkflag)
{
cBox.setVisibility(View.VISIBLE);
}
else
{
cBox.setVisibility(View.GONE);
}
cBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(cBox.isChecked())
{
selectedIds.add(recIdArr.get(reportslistview.getPositionForView(cBox)));
//System.out.println("position "+reportslistview.getPositionForView(cBox));
}
else
{
selectedIds.remove(recIdArr.get(reportslistview.getPositionForView(cBox)));
}
}
});
return row;
}
}
检查====>
for(int i=0;i<selectedIds.size();i++)
{
System.out.println("delete multiple"+selectedIds.size()+" "+Integer.parseInt(selectedIds.get(i)));
}
声明selectedIds
为全局变量
于 2012-08-13T09:03:23.903 回答
2
对我有用的非常简单的解决方案:
像这样,您可以通过对 ArrayAdapter 使用已经存在的 ArrayList 来遍历 ListView。
for(int i = 0; i < ArrayList.size();i++){
if((CheckBox)listView.getChildAt(i).findViewById(R.id.checkBox) != null){
CheckBox cBox=(CheckBox)listView.getChildAt(i).findViewById(R.id.checkBox);
if(cBox.isChecked()){
Log.e("CB", ""+i);
}
}
}
于 2013-09-24T22:25:38.477 回答
1
您需要将 OnClickListener 添加到您的复选框(在您的适配器中)。当按下按钮时,此侦听器必须与您要删除的文件保持最新列表。
就像是 :
选中 => 添加到列表
未选中 => 从列表中删除
于 2012-08-13T09:02:06.757 回答
1
Listview list;
ArrayAdapter<String> aa=new ArrayAdapter<String>this,android.R.layout.simple_list_item_multiple_choice,"Arraylist name");
list.setAdapter(aa);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//code
}
于 2012-08-13T09:12:17.770 回答