次のように宣言された配列アダプターを持つ ListActivity がありarrayAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_checked);
ます。これは、右端にチェックマークが付いた一連の行を示しています。それらのチェックマークへの参照を取得する方法、またはそれらをチェック/チェック解除する方法を教えてください。
3 に答える
CheckedTextView 自体がチェックボックスを処理します。これは、onListItemClick ハンドラーの 2 番目の引数 (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)は現在表示されているアイテムを参照しますが、リストが画面に収まらないため、すべてのリストアイテムを選択するために使用できませんでした。さらに、CheckedTextViewのsetCheckedを使用すると、リストをスクロールするとすぐに選択範囲が表示されなくなるという問題がありました。ListViewのsetItemChecked(ソースコードのコメントも参照してください)。さらに、チェックされたアイテムを取得する方法を示します。
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>
私ができる最も近いのは、セルがクリックされた後にチェックマークを変更することです:
@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);
}
ユーザーがセルに触れずにチェックマークを設定できるようにしたいと思います。