2010-02-05 73 views
2

我有一個ListActivity與一個聲明爲arrayAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_checked);的數組適配器這顯示了在最右邊的一堆與選中標記的行。你能告訴我如何得到這些複選標記的參考或如何檢查/取消選中它們?如何激活列表活動中的複選標記?

回答

0

我能做的最接近的是改變使用複選標記單擊單元格後:

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) 
{ 
    CheckedTextView textView = (CheckedTextView)l.getChildAt(position); 
    text.setChecked(!textView.isChecked()); 

    super.onListItemClick (l, v, position, id); 
} 

我仍希望能夠設置複選標記,而無需用戶接觸任何細胞。

8

CheckedTextView本身處理複選框。它作爲onListItemClick處理程序中的第二個參數(View v)傳入。所以,你可以簡化你的代碼如下:

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) 
{ 
    CheckedTextView textView = (CheckedTextView)v; 
    textView.setChecked(!textView.isChecked()); 
} 
+2

注意,這可能更有意義使用'l.setItemChecked(!位置,l.isItemChecked(位置)); '如果你的列表視圖延伸'Checkable'! (他們爲android.R.layout.simple_list_item_checked所做的)。如果你只檢查'CheckedTextView',它不會更新底層數據,例如isItemChecked()不會改變。 – Maarten 2012-10-03 19:31:41

2

我也有類似的問題,並試圖在這裏提供的解決方案,但我還是有很多的問題。 我只想要一個帶有可選「測試用例」和兩個按鈕「選擇所有測試」和「運行選定測試」的列表(因此我不想只有一個ListActivity)。 正如「JDC」 getChildAt(和getChildCount)提到參考當前顯示項目,但我的名單並不適合在屏幕上,所以我不能用它來選擇所有列表項。 此外,如果我使用setCheckedCheckedTextView我有問題,只要我滾動列表選擇消失。 我的解決方案是下面的代碼使用getCount將解決這些問題setItemChecked的的ListView(見源代碼中的註釋)。另外它顯示瞭如何檢索檢查的項目。

package com.example.test; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.util.SparseBooleanArray; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.CheckedTextView; 
import android.widget.ListView; 

public class TestActivity extends Activity { 

    static final String[] names = new String[] { "Test 1", "Test 2", "Test 3", "Test 4", "Test 5", "Test 6", "Test 7", "Test 8", "Test 9", "Test 10"}; 
    ListView list; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Create an ArrayAdapter, that will actually make the Strings above 
     // appear in the ListView 
     list = (ListView)findViewById(R.id.listOfTests); 
     list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, names)); 
    } 

// This does not work. 
// 1st: it checks only the displayed items 
// 2nd: as soon as you scroll the list the selections are undone 
// 
// public void onRunAllTestsClick (View view) { 
//  int count = list.getChildCount(); 
//  for (int i = 0; i < count; i++) 
//   ((CheckedTextView)list.getChildAt(i)).setChecked(true); 
// } 

    // This is the solution 
    // 1st: getCount deliveres the count of all list items (even if they are not displayed) 
    // 2nd: calling setItemChecked on the ListView ensures that the ListView "knows" that the item is checked and does not destroy it if you scroll the list 
    public void onRunAllTestsClick (View view) { 
     int count = list.getCount(); 
     for (int i = 0; i < count; i++) 
      list.setItemChecked(i, true); 
    } 

    public void onRunSelectedTestsClick (View view) { 
     SparseBooleanArray resultArray = list.getCheckedItemPositions(); 
     int size = resultArray.size(); 
     for (int i = 0; i < size; i++) 
      if (resultArray.valueAt(i)) 
       Log.i("CodecTestActivity", list.getAdapter().getItem(resultArray.keyAt(i)).toString()); 
    } 
} 

這裏也是適當的佈局(main.xml中):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/linearLayout1"> 
     <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onRunSelectedTestsClick" android:id="@+id/RunSelectedTestsClick" android:text="@string/runselectedtests"></Button> 
     <Button android:text="@string/runalltests" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onRunAllTestsClick" android:id="@+id/RunAllTests"></Button> 
    </LinearLayout> 
    <ListView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/listOfTests" android:choiceMode="multipleChoice"></ListView> 
</LinearLayout>