我正在阅读存储在手机中的联系人并在列表视图中显示它们。 我的代码如下:
String col[]={ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
Cursor cursorNames = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, col, null, null, null);
intentContactsToreturn=new Intent();
ArrayList<String> contactNames=new ArrayList<String>();
ArrayList<String> contactNumbers=new ArrayList<String>();
try
{
if(cursorNames.getCount() > 0)
{
while(cursorNames.moveToNext())
{
String id = cursorNames.getString(cursorNames.getColumnIndex(Contacts._ID));
contactNames.add(cursorNames.getString(cursorNames.getColumnIndex(Contacts.DISPLAY_NAME)));
Cursor cursorNumbers =getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
cursorNumbers.moveToFirst();
contactNumbers.add(cursorNumbers.getString(cursorNumbers.getColumnIndex(CommonDataKinds.Phone.NUMBER)));
}
}
}
finally
{
cursorNames.close();
}
ContactListArrayAdapter arrayAdapter = new ContactListArrayAdapter(this,contactNames,contactNumbers);
// lv.addFooterView(b);
listViewContacts.setAdapter(arrayAdapter);
}
现在问题是它正在读取SIM卡联系人(而不是手机通讯录),也没有读取SIM卡中的所有联系人。
它正在读取SIM中的一些联系人,确切数字是15(而在SiM中有100多个联系人),并且它还在列表视图中重复联系人。
像这样 15个联系人 然后,同样的15个联系人在列表视图中重新开始。一次又一次地重复总共4次15次接触。
答案 0 :(得分:1)
我使用此代码来获取联系人。
public ArrayList<String> c_Name = new ArrayList<String>();
public ArrayList<String> c_Number = new ArrayList<String>();
static String[] name_Val = null;
String[] phone_Val = null;
String local_value = null;
ContentResolver cr1 = getContentResolver();
String[] projection = new String[] { People._ID, People.NAME,
People.NUMBER };
Uri phone_contacts = People.CONTENT_URI;
Cursor managedCursor = cr1.query(phone_contacts, projection, null,
null, People.NAME + " ASC");
if (managedCursor.moveToFirst()) {
String contactname;
String cphoneNumber;
int nameColumn = managedCursor.getColumnIndex(People.NAME);
int phoneColumn = managedCursor.getColumnIndex(People.NUMBER);
Log.d("int Name", Integer.toString(nameColumn));
Log.d("int Number", Integer.toString(phoneColumn));
do {
contactname = managedCursor.getString(nameColumn);
cphoneNumber = managedCursor.getString(phoneColumn);
Log.d("Outside cname", "ts" + contactname);
Log.d("Outside cno", "ts" + cphoneNumber);
if ((contactname == " " || contactname == null)
&& (cphoneNumber == " " || cphoneNumber == null)) {
// displayAlert1();
} else {
c_Name.add(contactname);
c_Number.add(cphoneNumber);
Log.d("contact Name", c_Name.toString());
Log.d("contact No", c_Number.toString());
}
} while (managedCursor.moveToNext());
}
Uri contacts = Uri.parse("content://icc/adn");
Cursor managedCursor1 = cr1.query(contacts, null, null, null, null);
if (managedCursor1.moveToFirst()) {
String contactname;
String cphoneNumber;
int nameColumn = managedCursor1.getColumnIndex("name");
int phoneColumn = managedCursor1.getColumnIndex("number");
Log.d("int Name", Integer.toString(nameColumn));
Log.d("int Number", Integer.toString(phoneColumn));
do {
// Get the field values
contactname = managedCursor1.getString(nameColumn);
cphoneNumber = managedCursor1.getString(phoneColumn);
if ((contactname == " " || contactname == null)
&& (cphoneNumber == " " || cphoneNumber == null)) {
// displayAlert1();
} else {
c_Name.add(contactname);
c_Number.add(cphoneNumber);
}
} while (managedCursor1.moveToNext());
}
name_Val = (String[]) c_Name.toArray(new String[c_Name.size()]);
phone_Val = (String[]) c_Number.toArray(new String[c_Name.size()]);
然后将name_Val,phone_Val设置为列表视图。
ContactListArrayAdapter arrayAdapter = new ContactListArrayAdapter(this,name_Val,phone_Val );
// lv.addFooterView(b);
listViewContacts.setAdapter(arrayAdapter);