2012-06-15 99 views
0

我正在使用android 2.1平臺。我的代碼將顯示所有來自仿真器聯繫人列表中的人員的姓名號碼和電子郵件ID,通過使用此代碼,我將獲得上述要求。我在我的一些聯繫人模擬器,我創建了兩個更多的聯繫人(比如C和D)。但問題是訪問數據不正確?

問題1.如果我創建具有不電子郵件名稱和編號的新接觸,這種接觸將採取電子郵件ID從剛剛接觸到它。 例如:如果我創建了一個名稱C沒有電子郵件ID但D有電子郵件ID,D是已經在模擬器中的聯繫人。 C會收到D的電子郵件ID .C和D具有相同的電子郵件ID。

問題2.如果我再次形成具有名電子郵件地址而不是數量一個新的接觸,這種接觸將採取從我以前創建的接觸。 例如:如果我創建的名稱E沒有號碼,但它會佔用聯繫人C的號碼。所以C和E具有相同的號碼

我使用此代碼

public class GetAllDatas extends Activity { 

ListView lvItem; 
private Button btnAdd; 
String displayName="", emailAddress="", phoneNumber=""; 
ArrayList<String> contactlist=new ArrayList<String>(); 
ArrayAdapter<String> itemAdapter; 

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

    lvItem = (ListView)this.findViewById(R.id.lvitems); 
    btnAdd = (Button)this.findViewById(R.id.btnAddItem); 

    itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist); 
    lvItem.setAdapter(itemAdapter); 

    btnAdd.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      readContacts(); 
     } 
    }); 

} 

private void readContacts() 
{ 
    ContentResolver cr =getContentResolver(); 
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    while (cursor.moveToNext()) 
    { 
     displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));  
     String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 


     Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null); 
     while (emails.moveToNext()) 
     { 
      emailAddress = emails.getString(emails.getColumnIndex(Email.DATA)); 
      break; 
     } 
     emails.close(); 


     if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
     { 
      Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null); 
      while (pCur.moveToNext()) 
      { 
       phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       break; 
      } 
      pCur.close(); 
     } 


     // To display the Details 
     contactlist.add(displayName+", "+phoneNumber+", "+ emailAddress+"\n"); 
     itemAdapter.notifyDataSetChanged(); 
    } 
    Collections.sort(contactlist); 
    cursor.close(); 
} 
} 

我怎樣才能解決這個問題?

回答

2

據我瞭解你的問題..

在引用變量之前添加新項目剛剛抹去前一.. 一樣,

emailAddress = "" ; 
displayName = "" ; 
phoneNumber = "" ; 

在獲取聯繫人信息,並將其添加while循環列出..

private void readContacts() 
{ 
    ContentResolver cr =getContentResolver(); 
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    while (cursor.moveToNext()) 
    { 
     emailAddress = "" ; 
     displayName = "" ; 
     phoneNumber = "" ; 

     displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));  
     String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 

     Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null); 
     while (emails.moveToNext()) 
     { 
      emailAddress = emails.getString(emails.getColumnIndex(Email.DATA)); 
      break; 
     } 
     emails.close(); 
     if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
     { 
      Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null); 
      while (pCur.moveToNext()) 
      { 
       phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       break; 
      } 
      pCur.close(); 
     } 

     // To display the Details 
     contactlist.add(displayName+", "+phoneNumber+", "+ emailAddress+"\n");   
    } 
    Collections.sort(contactlist); 
    itemAdapter.notifyDataSetChanged(); 
    cursor.close(); 
} 
+0

我不能讓你... – BigBoss

+0

只需使用上面的readContacts() 方法。 (替換爲你的)並讓我知道發生了什麼.. – user370305

+0

thank you user370305我得到了我期望的輸出結果,一件事是做的,一個聯繫人有2個數字和2個電子郵件Id(說名字A有2個數字和2個電子郵件)..你能夠顯示它像 A,firstNumber,firstEmail作爲第一行和A,第二個數字,第二個電子郵件作爲第二行在列表視圖 是可能的嗎? 這是現在出現的所有其他人都沒有了的唯一問題。 – BigBoss

0

試試這個..........

  public void readContacts(){ 
    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, 
      null, null, null, null); 

    contactName=new String[cur.getCount()]; 
    contactNumber=new String[cur.getCount()][]; 
    contactEmail=new String[cur.getCount()][]; 
    int i=0; 
    if (cur.getCount() > 0) { 
     while (cur.moveToNext()) { 
      String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
      contactName[i]=name; 
      Log.e("Contact Name",contactName[i]); 
      if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 


       // get the phone number 
       Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
             ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
             new String[]{id}, null); 
       contactNumber[i]=new String[pCur.getCount()]; 
       int j=0; 
       while (pCur.moveToNext()) { 
        String phone = pCur.getString(
          pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

       contactNumber[i][j]=phone; 
       Log.d("Contact number",contactNumber[i][j]); 
       j++; 
       } 
       pCur.close(); 
      } 
       // get email and type 

       Cursor emailCur = cr.query(
         ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
         new String[]{id}, null); 
       contactEmail[i]=new String[emailCur.getCount()]; 
       int k=0; 
       while (emailCur.moveToNext()) { 

        String email = emailCur.getString(
           emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
        String emailType = emailCur.getString(
           emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 

       contactEmail[i][k]=email; 
       Log.v("Contact email",contactEmail[i][k]); 
       k++; 
       } 
       emailCur.close(); 


      } 
     } i++; 
    } 
+0

Anu它很複雜 – BigBoss