程序的ListView視圖中設置有復選框。我想從ListView中獲取選中的項目,例如:MyFiles應用程序中用復選框選擇多個文件,然後點擊單個刪除按鈕就能刪除所有的文件。有什麼方法實現這樣的功能呢?
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聲明為全局變量。