i want to display my contacts in a recyclerview with cardelements. The problem is, that it is only displayed one contact for several times. So the same name is shown in every card. It seems like the cursor doesn't get to the next contact. But I don't know why. And I have to admit that I'm quite new to android. Here is the code:
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();
}
and my 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;
}
Thank you!