【问题标题】:Android ListView with custom layout getCheckedItemPositions具有自定义布局 getCheckedItemPositions 的 Android ListView
【发布时间】:2016-06-08 20:35:26
【问题描述】:

我有一个带有自定义布局的ListViewCheckBox 可以选择要删除的多个项目,但list.getCheckedItemPositions() 总是返回 0。

我找不到我错过了什么。当用户检查 CheckBox 为 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)); 
        }    
    }          
}

【问题讨论】:

  • 如果您使用自定义行布局,该方法将不起作用。但是你有那个布尔数组,为什么不使用它呢?
  • 如何使用它?通过公开?
  • 您可以将其公开或使用方法公开。

标签: android listview checkbox baseadapter


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    用途:

    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));
    }
    

    问候!

    【讨论】:

      猜你喜欢
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      相关资源
      最近更新 更多