0

複数選択できるリストビューを作成したいと思います。一般的な解決策は、カーソルを取得してSimpleCursorAdapterを使用することです。

        SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
                    R.layout.simple_list_item_multiple_choice, cur2, cols2, views2);

を使用すると、これを機能させることができR.layout.simple_list_item_multiple_choiceます。複数の項目を選択すると、チェックマークが機能します。

そこで、カスタムメイドのレイアウトで試してみることにしました。これが私のレイアウトのXMLコードです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/lookup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceSmall" />


    <TextView
        android:id="@+id/hasphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceSmall" />

    <CheckedTextView
        android:id="@+id/checkedTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:checkMark="?android:attr/listChoiceIndicatorMultiple"/>

</LinearLayout>

これが私の問題です。同じコードを使用し、リストビューでChoiceModeを複数に設定すると、レイアウトがうまく膨らみます。カーソルからのデータは正常に入力されます。

しかし、私が抱えている問題は、アイテムをクリックしたときにチェックマークが選択されたものとして表示されないことです(ボックスのチェックボックス)。カスタムアダプタの作成を伴わない、私が見逃しているものはありますか?


l2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

l2は私のリストビューです。

4

2 に答える 2

5

私はCheckedTextViewを使用していません...私の理解では、行をクリックするとチェックが切り替わるはずです。

私はあなたがこのようにそれを強制しようとすることができると思います:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {  
    CheckedTextView chkTxt = (CheckedTextView) v.findViewById(R.id.CheckedTextView1); 
    chkTxt.toggle(); 
}

これはハックであり、根本的な問題を実際に見つける必要があることに注意してください。ただし、その間に問題が発生する可能性があります。

編集

何度もグーグルした後、問題が見つかりました... CheckedTextViewを使用するには、行レイアウトのルートをチェック可能にする必要がありますが、LinearLayoutはチェック可能ではありません。

さらにグーグルで解決策を見つけました...LinearLayoutクラスをオーバーライドします。

手順/コードはこちら

于 2012-05-20T14:07:22.943 に答える
1

「私が抱えている問題は、チェックマークが選択されたとおりに表示されないことです」という問題は、間違ったレイアウトが原因です。LinearLayout なしである必要があります。

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        android:id="@+id/名前"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/ルックアップ"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceSmall" />


    <TextView
        android:id="@+id/hasphone"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceSmall" />

    <CheckedTextView
        android:id="@+id/checkedTextView1"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"

        android:checkMark="?android:attr/listChoiceIndicatorMultiple" />

その後、SimpleCursorAdapter は "checkedTextView1" を適切に設定できます (目に見える効果があります)。

私の場合、ArrayAdapter を使用しましたが、うまくいきました。

    list.setAdapter(new ArrayAdapter<String>(this, R.layout.category_layout,
                    R.id.category_checkedText, this.categories));

于 2012-07-08T15:49:47.263 に答える