【问题标题】:Picking contacts and displaying it in recyclerview选择联系人并在recyclerview中显示
【发布时间】:2019-02-22 11:02:09
【问题描述】:

设置联系人

public class SettingsContacts extends AppCompatActivity {
private RecyclerView contactsList;
private List<ContactsHelper> contacts = new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private AdapterContacts mAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings_contacts);

    contactsList = (RecyclerView) findViewById(R.id.usersList);
    //Add the data first
    addDataToList();
    linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    //and then create a object and pass the lis
    mAdapter = new AdapterContacts(contacts);

    contactsList.setHasFixedSize(true);
    contactsList.setLayoutManager(linearLayoutManager);
    contactsList.setAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();


}

public void addDataToList(){
    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
    if (cursor != null) {
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                if (hasPhoneNumber > 0) {
                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    Cursor phoneCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id},
                            null);
                    if (phoneCursor != null) {
                        if (phoneCursor.moveToNext()) {
                            String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                            contacts.add(new ContactsHelper(name, phoneNumber));
                            phoneCursor.close();
                        }
                    }
                }
            }
        }
        cursor.close();
    }
}
}

适配器联系人

ppublic class AdapterContacts extends RecyclerView.Adapter<AdapterContacts.ContactViewHolder>{

private List<ContactsHelper> mContacts;
private DatabaseReference mDatabaseReference;
private FirebaseAuth mAuth;

public AdapterContacts(List<ContactsHelper>mContacts)

{
    this.mContacts = mContacts;
}

public AdapterContacts(String name, String phoneNumber) {
}

public class ContactViewHolder extends RecyclerView.ViewHolder{
    public TextView nameText;
    public TextView phonenumberText;
    public ContactViewHolder(View view)
    {
        super(view);
        nameText = (TextView)view.findViewById(R.id.contact_text_layout);
        phonenumberText = (TextView)view.findViewById(R.id.contact_text_layout2);
    }
}
@Override
public AdapterContacts.ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_activity_contact,parent,false);
    mAuth = FirebaseAuth.getInstance();
    mDatabaseReference = FirebaseDatabase.getInstance().getReference();
    return new AdapterContacts.ContactViewHolder(V);


}

@Override
public void onBindViewHolder(final AdapterContacts.ContactViewHolder holder, int position) {


    ContactsHelper contacts = mContacts.get(position);
    String name = contacts.getName();
    String phoneNumber = contacts.getPhoneNumber();
    holder.nameText.setText(name);
    holder.phonenumberText.setText(phoneNumber);

}

@Override
public  int getItemCount() {
    return mContacts.size();
}

}

联系人助手

public class ContactsHelper {
private String Name;
private String PhoneNumber;

public ContactsHelper() {
}

public ContactsHelper(String Name, String PhoneNumber) {
    this.Name = Name;
    this.PhoneNumber = PhoneNumber;
}
public String getName() {
    return Name;
}

public void setName(String Name) {
    this.Name = Name;
}

public String getPhoneNumber() {
    return PhoneNumber;
}

public void setPhoneNumber(String PhoneNumber) {
    this.PhoneNumber = PhoneNumber;
}

}

对不起,但我搞砸了...就像我找到了获取所有联系人的代码...但是我不知道如何实现它并在回收站视图中显示它...我是新手所以谁能帮帮我...提前谢谢...另外,如果您需要xml布局的代码,请询问...

【问题讨论】:

  • 你必须在创建适配器对象之前将数据添加到arraylist
  • 你能回答一下吗?因为我完全失去了@FaizMir
  • 您现在面临的问题是什么?
  • 适配器为空
  • @MichelleKinsten 你能删除这行并检查contactsList.setHasFixedSize(true);

标签: java android xml android-studio android-recyclerview


【解决方案1】:

尝试在while循环之后添加mAdapter.notifyDataSetChanged();

while(cursor.moveToNext()) {
    .
    .
    .
}
mAdapter.notifyDataSetChanged();

编辑:

在将contacts 传递给mAdapter 之前定义addDataToList() 并将addDataToList() 移动到底部。

contactsList = (RecyclerView) findViewById(R.id.usersList);
//Add the data first
linearLayoutManager = new LinearLayoutManager(getApplicationContext());
//and then create a object and pass the lis

final ArrayList<Contacts> contacts = new ArrayList<>();
mAdapter = new AdapterContacts(contacts);

contactsList.setHasFixedSize(true);
contactsList.setLayoutManager(linearLayoutManager);
contactsList.setAdapter(mAdapter);
addDataToList();

【讨论】:

  • 查看编辑,如果您仍有问题,请告诉我
【解决方案2】:

您正在向适配器添加一个空列表:

mAdapter = new AdapterContacts(contacts);

这样做:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings_contacts);

    contactsList = (RecyclerView) findViewById(R.id.usersList);
    linearLayoutManager = new LinearLayoutManager(getApplicationContext());

    //HERE add the data in the contacts array before add it into the adapter
    contacts = getContacts();
    mAdapter = new AdapterContacts(contacts);

    contactsList.setHasFixedSize(true);
    contactsList.setLayoutManager(linearLayoutManager);
    contactsList.setAdapter(mAdapter);
}


private ArrayList<Contacts> getContacts(){
    ArrayList<Contacts> contactList = new ArrayList<Contacts>();
    ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        if (cursor != null) {
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                    if (hasPhoneNumber > 0) {
                        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        Cursor phoneCursor = contentResolver.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id},
                                null);
                        if (phoneCursor != null) {
                            if (phoneCursor.moveToNext()) {
                                String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                                contactList.add(new Contacts(name, phoneNumber));
                                phoneCursor.close();
                            }
                        }
                    }
                }
            }
            cursor.close();
        }
return contactList;

}

【讨论】:

  • 什么是退货声明'
  • 对不起,我犯了大错。查看已编辑的答案。
【解决方案3】:

试试这个:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings_contacts);

    contactsList = (RecyclerView) findViewById(R.id.usersList);
    //Add the data first 
    addDataToList();
    linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    //and then create a object and pass the lis
    mAdapter = new AdapterContacts(contacts);

    contactsList.setHasFixedSize(true);
    contactsList.setLayoutManager(linearLayoutManager);
    contactsList.setAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();


}
public void addDataToList(){
final ArrayList<Contacts> contacts = new ArrayList<>();
    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
    if (cursor != null) {
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                if (hasPhoneNumber > 0) {
                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    Cursor phoneCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id},
                            null);
                    if (phoneCursor != null) {
                        if (phoneCursor.moveToNext()) {
                            String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                            contacts.add(new Contacts(name, phoneNumber));
                            phoneCursor.close();
                        }
                    }
                }
            }
        }
        cursor.close();
    }
}
}

全局声明

 ArrayList<Contacts> contacts = new ArrayList<>();

【讨论】:

  • @Arcana 请再次检查他的代码,让我知道他是否添加了一个空列表
  • 我刚刚在contacts.add(new Contacts(name,phonenumber))下找到了一行;它在适配器中创建了一个空的构造函数 public AdapterContacts(String name, String phoneNumber) { }
  • 还是空的
  • 请记录您的数组列表并查看其是否包含数据
  • 适配器是空的,我猜是因为我在 while 循环之后添加了 notifydatasetchanged 并在该行出现错误,所以这可能是我的问题......我也编辑了我的问题,现在它多了一点我想很清楚,所以请参考并告诉我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
  • 2015-09-27
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
相关资源
最近更新 更多