2014-04-03 98 views
1

我想修改我的ListView使其:创建多选择自定义的ListView

  1. 包含图片
  2. 包含文字说明(文件名)
  3. 包含一个复选框。

该列表应该是多选的。我应该能够遍历所有选中的复选框并获取描述。

目前,我可以用复选框显示一个普通的ListView。

ArrayAdapter<String> ad = new ArrayAdapter<String>(HelloDropboxActivity.this, android.R.layout.simple_list_item_multiple_choice,fileName); 
       mTestListOutput.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
       mTestListOutput.setAdapter(ad); 

使用的onclick一个Button - 我可以通过检查箱子循环并获得文成这样一个ArrayList:

ArrayList<String> dbLinks = new ArrayList<String>(); 
int cntChoice = mTestListOutput.getCount(); 
    SparseBooleanArray sparseBooleanArray = mTestListOutput.getCheckedItemPositions(); 

       for(int i = 0; i < cntChoice; i++) 
       { 
        if(sparseBooleanArray.get(i) == true) 
        { 

         dbLinks.add(mTestListOutput.getItemAtPosition(i).toString()); 
        } 
       } 

我retreving的ArrayList: 私人ArrayList的路径;

那么我该如何将这些绑在一起来创建一个自定义的ListView? 我看了很多例子,并试图修改它们,但我无处可去。

这里我attampted在我list_row.xnl创建列表行的布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 

    android:orientation="horizontal" 
    android:padding="5dip" > 

    <!-- Thumbnail image --> 
    <LinearLayout android:id="@+id/thumbnail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="3dip" 
     android:layout_alignParentLeft="true" 

     android:layout_marginRight="5dip"> 

     <ImageView 
      android:id="@+id/list_image" 
      android:layout_width="50dip" 
      android:layout_height="50dip" 
      /> 

    </LinearLayout> 

<!-- File Name --> 
    <TextView 
     android:id="@+id/filename" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/title" 
     android:textColor="#343434" 
     android:textSize="10dip" 
     android:layout_marginTop="1dip" 
     android:layout_toRightOf="@+id/thumbnail" 
     android:text="Just gona stand there and ..." /> 

</RelativeLayout> 

回答

0

我认为遇到的问题是,在选择列表项时,实际上是被选定的,但您看不到任何区别,因为根视图必须实现“可检查”。尽管我认为它应该是更复杂的,但还是有办法做到这一点。

首先,您需要添加以下类。这是一个可检测的LinearLayout,它应该将来自可检查接口的调用中继到其中的复选框。对于要使用的任何视图组,应该很容易进行修改。

public class CheckableLinearLayout extends LinearLayout implements Checkable { 
private CheckBox _checkbox; 

public CheckableLinearLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@Override 
protected void onFinishInflate() { 
    super.onFinishInflate(); 
    // find checked text view 
    int childCount = getChildCount(); 
    for (int i = 0; i < childCount; ++i) { 
     View v = getChildAt(i); 
     if (v instanceof CheckBox) { 
      _checkbox = (CheckBox) v; 
     } 
    } 
} 

public boolean isChecked() { 
    return _checkbox != null ? _checkbox.isChecked() : false; 
} 

public void setChecked(boolean checked) { 
    if (_checkbox != null) { 
     _checkbox.setChecked(checked); 
    } 
} 

public void toggle() { 
    if (_checkbox != null) { 
     _checkbox.toggle(); 
    } 
} 
} 

接下来定义要用于每个项目的,布局,使用上述CheckableLinearLayout:

<com.myPackage.CheckableLinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

<CheckBox 
    android:id="@+id/checkBox1" 
    android:focusable="false" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="CheckBox" /> 

<TextView 
    android:id="@+id/textView1" 
    android:focusable="false" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

<ImageView 
    android:id="@+id/imageView1" 
    android:focusable="false" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/background_iv" /> 

</com.myPackage.CheckableLinearLayout> 

然后,改变创建的适配器的线,使得该布局的ID以上被用来代替。

+0

谢谢我会试试这个 - 但什么是私人CheckedTextReadventureView _checkbox,以及如何更改我的代码使用它?和_checkbox.isChecked()定义在哪里? – user3437721

+0

那是我的一个自定义复选框,我没有注意到当我复制代码时。现在更改答案,以便它使用标准复选框。 – CurlyPaul

+0

谢谢 - 我是否需要在xml中更改它以成为我的包,例如 user3437721