我有一個ListActivity與一個聲明爲arrayAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_checked);
的數組適配器這顯示了在最右邊的一堆與選中標記的行。你能告訴我如何得到這些複選標記的參考或如何檢查/取消選中它們?如何激活列表活動中的複選標記?
2
A
回答
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
我也有類似的問題,並試圖在這裏提供的解決方案,但我還是有很多的問題。 我只想要一個帶有可選「測試用例」和兩個按鈕「選擇所有測試」和「運行選定測試」的列表(因此我不想只有一個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>
相關問題
- 1. 如何激活div標記
- 2. HTML選擇標記激活和關閉
- 3. 通過布爾複選標記激活表單中的字段 - Odoo v8
- 4. 如何激活標籤IOS
- 5. 激活/取消激活按鈕定位標記
- 6. HLA中的激活記錄
- 7. 如何激活點擊標籤和不活動的默認值?
- 8. 激活並取消激活首選項
- 9. 如何激活動畫隊列
- 10. jQuery標籤手動激活
- 11. 如何激活類onclick列表項
- 12. 如何取消激活aui功能:複選框標籤?
- 13. MDL複選框問題:不希望標籤激活複選框
- 14. 如何激活保持活動狀態,http://www.webpagetest.org表示保持活動狀態未激活
- 15. 未激活活動中的onKey
- 16. 重新激活活動
- 17. Android部分激活活動
- 18. 在列表視圖中自動激活一個選項
- 19. 灰掉下拉列表,選擇選項時動態激活
- 20. 如何激活表中的bootstrap datepicker?
- 21. 如何激活此表中的排序?
- 22. 根據SAPUI5中的複選框標誌激活輸入字段
- 23. (De-)使用單獨框激活複選框列表
- 24. 如何使用django中的複選框激活charfield?
- 25. 如何取消激活選擇並激活Flot中的縮放/平移?
- 26. 如何突出顯示活動表中的活動選項?
- 27. Sharepoint 2010 - 「標記和備註」未激活
- 28. Google Maps Api標記羣集激活?
- 29. OpenOPC標籤激活?
- 30. 如何從tabview中的列表活動開始新的活動
注意,這可能更有意義使用'l.setItemChecked(!位置,l.isItemChecked(位置)); '如果你的列表視圖延伸'Checkable'! (他們爲android.R.layout.simple_list_item_checked所做的)。如果你只檢查'CheckedTextView',它不會更新底層數據,例如isItemChecked()不會改變。 – Maarten 2012-10-03 19:31:41