我正在使用以下代码使用SimpleCursorAdapter检索联系人姓名和号码。
final Uri uri = ContactsContract.Contacts.CONTENT_URI;
final String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
// boolean mShowInvisible = false;
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP
+ " = '1'";
String[] selectionArgs = null;
final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
m_curContacts = managedQuery(uri, projection, selection, selectionArgs,
sortOrder);
String[] fields = new String[] { ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
m_slvAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, m_curContacts, fields,
new int[] { android.R.id.text1,android.R.id.text2 });
但是当我运行它时,我收到以下错误
04-07 17:05:34.980: WARN/dalvikvm(614): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-07 17:05:34.990: ERROR/AndroidRuntime(614): Uncaught handler: thread main exiting due to uncaught exception
04-07 17:05:35.080: ERROR/AndroidRuntime(614): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kpj4s.contactadapter/com.kpj4s.contactadapter.ContactAdapter}: **java.lang.IllegalArgumentException: column 'data1' does not exist**
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.os.Looper.loop(Looper.java:123)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at java.lang.reflect.Method.invoke(Method.java:521)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at dalvik.system.NativeStart.main(Native Method)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): Caused by: java.lang.IllegalArgumentException: column 'data1' does not exist
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:339)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:91)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at com.kpj4s.contactadapter.ContactAdapter.ReadContacts(ContactAdapter.java:158)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at com.kpj4s.contactadapter.ContactAdapter.onCreate(ContactAdapter.java:132)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): ... 11 more
当我删除 ContactsContract.CommonDataKinds.Phone.NUMBER 时,一切正常。当我使用那段代码时,错误就会出现。
有没有办法通过这个或那个传递来获取联系人电话号码和姓名使用SimpleCursorAdapter?
请帮助我们......
提前感谢...
答案 0 :(得分:0)
使用此代码获取电话号码和姓名。
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()]);