2015-05-06 34 views
1

我想用cardelements在回收站中顯示我的聯繫人。問題是,它只是多次顯示一個聯繫人。所以每張卡都顯示相同的名字。好像光標沒有到達下一個聯繫人。但我不知道爲什麼。我不得不承認,我對android非常陌生。 下面是代碼:帶有Cardview的RecyclerView中的ContactList

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.contacts); 
    RecyclerView recList = (RecyclerView) findViewById(R.id.cardList); 
    recList.setHasFixedSize(true); 
    LinearLayoutManager llm = new LinearLayoutManager(this); 
    llm.setOrientation(LinearLayoutManager.VERTICAL); 
    recList.setLayoutManager(llm); 


    contactRecAdapter ca = new contactRecAdapter(data); 
    recList.setAdapter(ca); 
    data = displayContact(); 
} 

和我displayContact():

private ArrayList<contactInfo> displayContact(){ 

    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null); 
    String name ; 
    String number; 
    String id; 

    contactInfo cI = new contactInfo(); 

    if(cur.getCount()>1){ 
     while (cur.moveToNext()){ 
      id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
      name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

      if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))>0){ 

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

       while (pCur.moveToNext()){ 
        number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        cI.number =number; 
       } 
       data.add(cI); 
       pCur.close(); 
      } 

     } 
    } 

    cur.close(); 
    return data; 
} 

謝謝!

+0

嘿,你需要先收集數據,然後將其設置在適配器中。 試着改變你的onCreate() data = displayContact(); contactRecAdapter ca = new contactRecAdapter(data); recList.setAdapter(ca); –

回答

0

您需要了解的是,目前,您有1個contactInfo對象,並且您將此contactInfo對象添加到數據(arraylist)中。

讓我們一次一個腳印。這是代碼正在執行的操作:

  1. 創建一個contactInfo對象(我們將其命名爲C1)。
  2. 將名稱設置爲「abc」,號碼設置爲「123」。
  3. 將此C1添加到數據列表列表中。 - >本質上,數組列表持有一個引用,指向此C1駐留在內存中(即它只指向C1)
  4. 光標創建另一個聯繫人並將C1的名稱設置爲「def」並將編號設置爲「456」
  5. 將此C1添加到數據列表列表中。 (此時,arraylist包含的實際上是2個引用,都指向C1)
  6. 假設遊標下一個找不到任何東西,最後返回數據列表列表。

然後,您將獲得一個包含2條記錄並且都由聯繫人名稱「def」和數字「456」組成的數組列表。

相反,我建議根據您的代碼建議,在設置名稱之前,每次找到新聯繫人時只需添加1行來創建新的contactInfo對象。

private ArrayList<contactInfo> displayContact(){ 

    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null); 
    String name ; 
    String number; 
    String id; 
    contactInfo cI; //Remove the creation of cI object at the start 

    if(cur.getCount()>1){ 
     while (cur.moveToNext()){ 
      id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
      name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

      if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))>0){ 

       cI = new contactInfo(); //Add in this to create a new cI object when there's a new contact 

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

       while (pCur.moveToNext()){ 
        number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        cI.number =number; 
       } 
       data.add(cI); 
       pCur.close(); 
      } 

     } 
    } 

    cur.close(); 
    return data; 
} 

,你可能想有它的名字作爲當前的方式,如果該數據是巨大需要的時間公平位一起取的電話號碼來優化這個代碼一點。你可以看看這篇文章:https://stackoverflow.com/a/28690030/3717990