2012-09-22 47 views
1

我試圖讓我的聯繫人在列表視圖上。現在我知道使用simple_list_item_multiple_choice可以讓我選擇多個聯繫人,但它僅查看沒有數字的姓名。Android:啓用了多項選擇和子項目的ListView

另一方面,simple_list_item_2可用於顯示姓名和號碼,但支持只選擇一個聯繫人。

是否有任何模板將它們結合在一起?如果不是的話,我怎樣才能用這兩種功能構建自定義列表?

編輯:這是我使用

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的第二個參數是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_NAMENUMBER要寫入android.R.id.text1android.R.id.text2,但不能使用多選功能。

+1

你可以把你使用的一些代碼?這將有很大幫助。 – Heejin

+1

您應該根據您的要求定製您的佈局。 – Dilip

+0

Heejin,完成。謝謝:) –

回答

1

正如Dipu所說,您應該製作自己的自定義佈局。 要顯示姓名和聯繫人,您需要兩個文本視圖和一個用於檢查的複選框。

您可以從本教程編碼:

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

再添加一個文本視圖,country_info.xml將解決你的問題。

ADDED

要使用自定義列表視圖佈局,你必須實現自己的適配器。

本教程(2.自定義ArrayAdapter示例)將幫助您弄清楚如何做到這一點。

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

0

由Heejin提供的答案是優秀的,但要實現自定義ArrayAdaptor這並不重要。我只需要編寫自定義佈局並將其發送到構造函數SimpleCursorAdaptor

自定義佈局表示列表視圖中每個項目的佈局。我需要每行包含CheckedTextView和另一個小TextView作爲子項。所以我做了一個名爲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(); 
    } 
} 

請注意,我有兩個佈局,一個用於列表視圖本身(名爲multiple_contacts),此處提供的佈局(稱爲row_view)是列表視圖中每個項目的佈局。從multiple_contacts我需要的所有東西是寫setContentView(R.layout.multiple_contacts);