【问题标题】:ContactList in RecyclerView with CardviewRecyclerView 中的联系人列表与 Cardview
【发布时间】:2015-05-06 05:39:21
【问题描述】:

我想在带有卡片元素的回收站视图中显示我的联系人。问题是,它只显示一个联系人多次。因此,每张卡片中都会显示相同的名称。似乎光标没有到达下一个联系人。但我不知道为什么。我不得不承认我对 android 很陌生。 代码如下:

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;
}

谢谢!

【问题讨论】:

  • 你好,你需要先收集数据,然后在适配器中设置。尝试更改您的 onCreate() data = displayContact(); contactRecAdapter ca = new contactRecAdapter(data); recList.setAdapter(ca);

标签: cursor contacts android-recyclerview android-cardview


【解决方案1】:

您需要了解的是,目前,您有 1 个contactInfo 对象,并且您正在将此contactInfo 对象添加到数据(数组列表)中。

让我们一步一步来。这是代码当前正在执行的操作:

  1. 创建一个contactInfo对象(我们将其命名为C1)。
  2. 将名称设置为“abc”,编号设置为“123”。
  3. 将此 C1 添加到数据数组列表中。 --> 本质上,arraylist 持有对这个 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

【讨论】:

    猜你喜欢
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    相关资源
    最近更新 更多