2

我有一個聯繫人選擇器列表,其中包含具有電話號碼的聯繫人的存儲箱。 現在,我的問題是,似乎無法得到檢查聯繫人的姓名和電話號碼。多個聯繫人選擇器列表

這裏是我的代碼:

import android.app.ListActivity; 
    import android.content.ContentResolver; 
    import android.content.Intent; 
    import android.database.Cursor; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.provider.ContactsContract; 
    import android.provider.ContactsContract.CommonDataKinds.Phone; 
    import android.util.Log; 
    import android.util.SparseBooleanArray; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.ListAdapter; 
    import android.widget.ListView; 
    import android.widget.SimpleCursorAdapter; 

    public class Create_Group extends ListActivity implements OnClickListener{ 

     // List variables 
     public String[] Contacts = {}; 
     public int[] to = {}; 
     public ListView myListView; 

     Button save_button; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.create_group); 

      // Initializing the buttons according to their ID 
      save_button = (Button)findViewById(R.id.save_group_button); 

      // Defines listeners for the buttons 
      save_button.setOnClickListener(this); 

      Cursor mCursor = getContacts(); 
      startManagingCursor(mCursor); 

      ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, mCursor, 
                  Contacts = new String[] {ContactsContract.Contacts.DISPLAY_NAME }, 
                  to = new int[] { android.R.id.text1 }); 
      setListAdapter(adapter); 
      myListView = getListView(); 
      myListView.setItemsCanFocus(false); 
      myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

     } 

     private Cursor getContacts() { 
      // Run query 
      Uri uri = ContactsContract.Contacts.CONTENT_URI; 
      String[] projection = new String[] { ContactsContract.Contacts._ID, 
              ContactsContract.Contacts.DISPLAY_NAME}; 
      String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '" 
        + ("1") + "'"; 
      String[] selectionArgs = null; 
      String sortOrder = ContactsContract.Contacts.DISPLAY_NAME 
        + " COLLATE LOCALIZED ASC"; 

      return managedQuery(uri, projection, selection, selectionArgs, 
        sortOrder); 
     } 

     public void onClick(View src) { 
      Intent i; 
      switch (src.getId()) 
      { 
      case R.id.save_group_button: 

       int checked_Names_Counter = 0; 

       // Goes over the list of contacts and checks which were checked 
       for (int j = 0; j < myListView.getCount(); j++) 
       { 
        if (myListView.isItemChecked(j) == true) 
        { 
         Cursor cur = getContacts(); 
         ContentResolver contect_resolver = getContentResolver(); 
         cur.moveToFirst(); 

         /** 
         * Here I tried to compare the IDs but each list has different IDs so it didn't really help me... 
         // Converts the current checked name ID into a String 
         String Checked_ID = String.valueOf(myListView.getCheckedItemIds()[checked_Names_Counter]); 

         // Checks if the current checked ID matches the cursors ID, if not move the cursor to the next name 
         while (Checked_ID != cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID))) 
         { 
          cur.moveToNext(); 
         } 
         */ 

         /** 
         * Here I tried to compare the names, even though it's not a good pratice, and it didn't work either... 
         String Checked_Name = myListView.getAdapter().getItem(checked_Names_Counter).toString(); 

         // Checks if the current checked ID matches the cursors ID, if not move the cursor to the next name 
         while (Checked_Name != cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME))) 
         { 
          cur.moveToNext(); 
         } 
         */ 
         String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID)); 
         String name = ""; 
         String no = ""; 

         Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
           ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); 


         name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
         no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

         id = null; 
         name = null; 
         no = null; 
         phoneCur = null; 
         checked_Names_Counter++; 

        } 
       } 

       // Goes back to the Manage Groups screen 
       i = new Intent(this, Manage_Groups.class); 
       startActivity(i); 
       break; 
     } 

     } 
    } 

任何想法?

謝謝!

回答

2

看起來你是如此接近,我用ListView.getCheckedItemIds()返回所選聯繫人的唯一ID:

public void onClick(View view) { 
    long[] ids = myListView.getCheckedItemIds(); 
    for(long id : ids) { 
     Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
       ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id + "" }, null); 

     // Do whatever you want with the data 
    } 
} 

加成

我有一個快速的問題這段代碼:

// Goes back to the Manage Groups screen 
i = new Intent(this, Manage_Groups.class); 
startActivity(i); 

這是否帶給用戶回到以前活動?如果是這樣,您應該使用finish();。 finish()結束當前Activity,將其從堆棧中釋放並釋放所有內存(浪費的內存減少意味着更快的應用程序)。它還允許先前的Activity在其離開時恢復保存的狀態(填充EditTexts,之前的Spinner選項,切換按鈕和複選標記等)。活動從用戶停止的地方恢復。

+0

是的,它確實返回到之前的活動,我將添加您所說的內容。另外,我只檢查了您簡要發佈的內容,但我認爲它效果很好,稍後我會完成檢查。謝謝!!! – user1476876

+0

@ user1476876如果這個答案對您有幫助,請點擊左上角的複選標記。 – Sam

+2

作爲其他人看到此答案的警告。您不應該在主線程上調用ContentResolver.query。由於調用將執行文件IO,因此您有冒險獲得ANR的風險。將聯繫人加載到加載程序或AsyncTask或類似程序中。您還可以編輯查詢以在一個查詢中加載所有相關聯繫人,而不是每個聯繫人一個查詢。 –

相關問題