1

リスト ビューで連絡先を取得しようとしています。を使用すると複数の連絡先を選択できることがわかりsimple_list_item_multiple_choiceましたが、数字のない名前のみが表示されます。

反対側でsimple_list_item_2は、名前と番号の両方を表示するために使用できますが、1 つの連絡先のみを選択できます。

両方を組み合わせたテンプレートはありますか?そうでない場合、両方の機能を備えたカスタマイズされたリストを作成するにはどうすればよいですか?

編集:これは私が使用しているコードです

CursorLoader cl = new CursorLoader(this,ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
Cursor c = cl.loadInBackground();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, // Use a template
                                                         // that displays a
                                                         // text view
                    c, // Give the cursor to the list adapter
                    new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
                    new int[] { android.R.id.text1},0); 

setListAdapter(adapter);

ここで、SimpleCursorAdapter の 2 番目のパラメーターは ですが、これは のsimple_list_item_multiple_choice処理のみをサポートしていandroid.R.id.text1ます。したがって、サブアイテムではなく、アイテムのみを使用できます。

しかし、次のコードでは

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_2, // Use a template
                                                         // that displays a
                                                         // text view
                    c, // Give the cursor to the list adapter
                    new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ,ContactsContract.CommonDataKinds.Phone.NUMBER},
                    new int[] { android.R.id.text1,android.R.id.text2},0); 

と の両方ContactsContract.CommonDataKinds.Phone.DISPLAY_NAMEを指定してとNUMBERで書くことはできますが、複数選択機能は使用できません。android.R.id.text1android.R.id.text2

4

2 に答える 2

2

Dipu が言ったように、独自のカスタマイズされたレイアウトを作成する必要があります。名前と連絡先を表示するには、2 つのテキスト ビューと、チェック用のチェック ボックスが 1 つ必要です。

このチュートリアルからコーディングを開始できます。

http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html

country_info.xml にテキスト ビューをもう 1 つ追加すると、問題が解決します。

追加した

カスタム リスト ビュー レイアウトを使用するには、独自のアダプターを実装する必要があります。

このチュートリアル (2. カスタム ArrayAdapter の例) は、その方法を理解するのに役立ちます。

http://www.mkyong.com/android/android-listview-example/

于 2012-09-22T11:17:42.097 に答える
0

Heejin によって提供された回答は優れていますが、カスタム ArrayAdaptor を実装することは重要ではありません。私がする必要があったのは、カスタム レイアウトを作成してSimpleCursorAdaptorコンストラクターに送信することだけでした。

カスタム レイアウトは、リスト ビュー内の各項目のレイアウトを表します。各行に aCheckedTextViewと別の smallTextViewをサブアイテムとして含める必要があります。そこで、次の内容のrow_view.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" >

    <CheckedTextView
        android:id="@+id/checkedTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:text="CheckedTextView" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

次に、コンストラクターで使用しました

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row_view, // Use a template
                                                         // that displays a
                                                         // text view
                    c, // Give the cursor to the list adapter
                    new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},
                    new int[] { R.id.checkedTextView, R.id.textView},0); 

setListAdapter(adapter);

これは完全なコードです

public class MultipleContacts extends ListActivity implements OnItemClickListener {
    private static final String[] PROJECTION = new String[] {
        ContactsContract.CommonDataKinds.Phone._ID
        ,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
        ,ContactsContract.CommonDataKinds.Phone.NUMBER
    };

    SimpleCursorAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.multiple_contacts);
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        getListView().setOnItemClickListener(this);
        // Get a cursor with all people

        CursorLoader cl = new CursorLoader(this,ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                PROJECTION, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");

        Cursor c = cl.loadInBackground();
        adapter = new SimpleCursorAdapter(this,
                R.layout.row_view, // Use a template
                                                     // that displays a
                                                     // text view
                c, // Give the cursor to the list adapter
                new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},
                new int[] { R.id.checkedTextView, R.id.textView},0); 

        setListAdapter(adapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        //what to do when an item is clicked
        CheckedTextView checkedTextView = (CheckedTextView) v.findViewById(R.id.checkedTextView);
        Toast.makeText(this, checkedTextView.getText(), Toast.LENGTH_SHORT).show();
    }   
}

2 つのレイアウトがあることに注意してください。1 つはリスト ビュー自体 ( と呼ばれmultiple_contactsます) で、ここで提供されるレイアウト ( と呼ばれますrow_view) は、リスト ビューの各項目のレイアウトです。私が必要multiple_contactsとするのは書くことだけですsetContentView(R.layout.multiple_contacts);

于 2012-09-22T14:04:41.223 に答える