2012-09-10 76 views
1

我在設置ListView中的多項選擇選項時遇到問題。我的ListView有聯繫人的名字,這是從Android手機檢索。無法在ListView中設置多項選擇選項

我已經按照設置多項選擇的例子,但它沒有解決。

我試着輸入

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views); 
     list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     this.setListAdapter(adapter); 

,但我沒有看到在ListView的複選框。

我已經用android.R.layout.simple_list_item_multiple_choice代替R.layout.contacts_list,它顯示了複選框,但沒有來自android手機通訊錄的名稱。

這裏是我的代碼: contacts_list.xml

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

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:stackFromBottom="false" 
     android:transcriptMode="normal" 
     android:choiceMode="multipleChoice" > 
    </ListView> 

    <TextView 
     android:textColor="@color/dark_purple" 
     android:id="@+id/contactName" 
     android:textStyle="bold" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

ContactsList.java

public class ContactsList extends ListActivity 
{ 
    final Context context = this; 

    Cursor cursor; 

    String[] buddiesList = 
     {"Kanak Priya", 
     "Joanne Liew", 
     "Michelle Lam", 
     "Teo Kin Hua", 
     "Melissa Haiting", 
     "David Yeo", 
     "Natasha Akhbar", 
     "Gillian Gan", 
     "Sonia", 
     "Ms Long", 
     "Joan Tang", 
     "Stephanie", 
     "Nur Ashiqin" 
     }; 

    BuddyDBAdapter buddyDB = new BuddyDBAdapter(this); 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.contacts_list); 

     ListView list = getListView(); 



     setListAdapter(new ArrayAdapter<String> (this, R.layout.contacts_list, R.id.contactName, buddiesList)); 

     Uri allContacts = Uri.parse("content://contacts/people"); 

     Cursor c = managedQuery(allContacts, null, null, null, null); 

     String[] columns = new String[] {ContactsContract.Contacts.DISPLAY_NAME}; 
     int[] views = new int[] {R.id.contactName}; 

     startManagingCursor(c); 

     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views); 
     list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     this.setListAdapter(adapter); 


     /*if(cursor.getCount()<0) 
     { 
      Intent displayIntent = new Intent(Intent.ACTION_VIEW); 
      displayIntent.setData(Uri.parse("content://contacts/people")); 
      startActivity(displayIntent); 

      displayIntent = new Intent(Intent.ACTION_VIEW); 
      //displayIntent.setData(Uri.parse("content://contacts/people/"+id)); 
      startActivity(displayIntent); 
     } 
     else 
     { 
      final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
      alertDialog.setTitle("ALERT!"); 
      alertDialog.setMessage("NO Name is Found! D:"); 
      alertDialog.setIcon(R.drawable.warning); 
      alertDialog.setButton("OK", new DialogInterface.OnClickListener() 
      { 
       public void onClick(DialogInterface dialog, int which) 
       { 
        Intent backIntent = new Intent(context, ContactsList.class); 
        startActivity(backIntent); 
       } 
      }); 
      alertDialog.show(); 
     }*/ 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) 
    { 
     buddyDB.open(); 
     long name_id; 
     super.onListItemClick(l, v, position, id); 


     Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor(); 

     TextView contactName = (TextView) v.findViewById(R.id.contactName); 
     String NameValue = contactName.getText().toString(); 

     name_id = buddyDB.insertNames(NameValue); 


     Toast.makeText(getBaseContext(), 
       "Selected: " + buddiesList[position], Toast.LENGTH_SHORT).show(); 


     buddyDB.close(); 

哦,順便說一下,在

String[] buddiesList = 
     {"Kanak Priya", 
     "Joanne Liew", 
     "Michelle Lam", 
     "Teo Kin Hua", 
     "Melissa Haiting", 
     "David Yeo", 
     "Natasha Akhbar", 
     "Gillian Gan", 
     "Sonia", 
     "Ms Long", 
     "Joan Tang", 
     "Stephanie", 
     "Nur Ashiqin" 
     }; 

是我的吐司消息。

我真的需要幫助。任何幫助將不勝感激。例子將不勝感激。
感謝

回答

3

您可以有兩個選擇:

1 - 從Android的資源使用默認佈局(simple_list_item_multiple_choice)

您可以將這段代碼:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.contacts_list, c, columns, views); 

發送至:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, c, columns, new int {android.R.id.text1}); 

2 - 創建新行佈局包含您定義的複選框,然後由你自己處理。

contact_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@color/light_goldenrod" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:stackFromBottom="false" 
     android:transcriptMode="normal" 
     android:choiceMode="multipleChoice" > 
    </ListView> 
</LinearLayout> 

contact_entry.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"> 
    <TextView 
     android:textColor="@color/dark_purple" 
     android:id="@+id/contactName" 
     android:textStyle="bold" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <CheckBox 
     android:id="@+id/contactCheckbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:checked="false" /> 

</LinearLayout> 

添加自定義光標適配器這樣的:

public class YourCursorAdapter extends CursorAdapter { 

private Context mContext; 
private Cursor mCurser; 

public YourCursorAdapter(Context context, int layout, Cursor c, String[] from, 
     int[] to, ContentResolver co) { 
    super(context, c); 
    mCurser = c; 
    mContext = context; 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    LayoutInflater inflater = LayoutInflater.from(mContext); 
    View v = inflater.inflate(R.layout.contact_entry, parent, false); 
    bindView(v, mContext, cursor); 
    return v; 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 

    TextView contactName= (TextView)view.findViewById(R.id.contactName); 
    contactName.setText(cursor.getString((mCurser 
      .getColumnIndexOrThrow(Contacts.Phones.DISPLAY_NAME)))); 
    CheckBox contactCheckbox = (TextView)view.findViewById(R.id.contactCheckbox); 
    contactCheckbox.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Checkbox checkBox = (Checkbox) v.findViewById(R.id.contactCheckbox) 
      if (checkBox.isChecked()) 
        checkBox.setChecked(false); 
      else 
        checkBox.setChecked(true); 
     } 
    }); 
} 
在ListActivity

YourCursorAdapter adapter = new YourCursorAdapter(this, 
      null, c, null, null,null); 
    this.setListAdapter(adapter); 
+0

嗨,我會試試你的代碼。如果我有任何問題,我會讓你知道。感謝您的回答。 – Preeyah

+0

我需要在Android資源 - res文件夾中創建'simple_list_item_multiple_choice'嗎? – Preeyah

+0

沒有。但它會與Android系統的默認列表視圖的行。如果你想實現自己的ListView的行,你必須通過添加複選框,它重新定義在res /佈局文件夾contacts_list.xml。 – PhatHV