我有一个ListView
自定义布局,CheckBox
可以选择多个要删除的项目,但list.getCheckedItemPositions()
总是返回 0。
我无法找出我错过了什么。CheckBox
当用户将 a 检查为 true 以便getCheckedItemPositions
返回它们时,如何填充检查项目列表?
列表显示
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/footer"
android:choiceMode="multipleChoice"
android:clickable="true"
android:layout_below="@+id/adViewHolder" />
排
<LinearLayout
android:id="@+id/recording_play_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"
android:orientation="vertical" >
<ImageView
android:id="@+id/recording_play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/play" />
<CheckBox
android:id="@+id/multi_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
适配器
public RecordingsAdapter(Context context, ListActivity a, ArrayList<MyObject> items) {
mContext = context;
mItems = items;
mActivity = a;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
checkBoxState=new boolean[items.size()];
}
..................
................
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView==null) {
vi = inflater.inflate(R.layout.recording_list_item, null);
}
final CheckBox multi_checkBox = (CheckBox) vi.findViewById (R.id.multi_checkBox);
multi_checkBox.setChecked(checkBoxState[position]);
multi_checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(((CheckBox)v).isChecked()) {
checkBoxState[position]=true;
v.setSelected(true);
} else{
checkBoxState[position]=false;
v.setSelected(false);
}
}
});
return vi;
}
活动
SparseBooleanArray checkedItemPositions = list.getCheckedItemPositions();
Log.i("recorded_file_delete list.getCheckedItemPositions()", String.valueOf(list.getCheckedItemPositions().size()));
int itemCount = list.getCount();
Log.i("recorded_file_delete", String.valueOf(itemCount));
for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
boolean deleted = f.delete();
if(deleted) {
Asr.recordingsCache.removeRecordingFile(f);
Log.i("recorded_file_delete", String.valueOf(deleted));
}
}
}