2014-04-03 156 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