0

我有一个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)); 
        }    
    }          
}
4

2 回答 2

1

您还需要将所选项目设置为列表视图已知。尝试使用这个- listView.setItemChecked(position,true);

于 2013-07-16T10:11:02.670 回答
0

利用:

SparseBooleanArray arraySparse = myGridView.getcheckeditempositions();

for(int i=0;i<arraySparse.size();i++){
//Shows values boolean of keys 
     Log.d("MI_APP",""+arraySparce.valueAt(i));
//Shows keys id or position of your element in the GridView
     Log.d("MI_APP",""+arraySparce.keyAt(i));
}

问候!

于 2013-11-12T06:30:28.417 回答