public class ImportContactsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact);
TextView txt = (TextView)findViewById(R.id.con);
String msg = "";
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(People.NUMBER));
String email = cursor.getString(cursor.getColumnIndexOrThrow(People.PRIMARY_EMAIL_ID));
msg += name + " " + number + " " + email + "\n";
}
txt.setText(msg.toString());
}
}
我正在开发一个应用程序,其中应用程序能够发送包含所有当前电话联系人(姓名+电话号码)的电子邮件作为备用替代方案。我正在尝试如何提取所有手机联系人信息并在TextView上显示它们......但我不能这样做,有人请咨询我。感谢...
LogCat
10-09 15:22:48.634: E/AndroidRuntime(680): FATAL EXCEPTION: main
10-09 15:22:48.634: E/AndroidRuntime(680): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.importcontacts/com.example.importcontacts.ImportContactsActivity} : java.lang.IllegalArgumentException: column 'name' does not exist
10-09 15:22:48.634: E/AndroidRuntime(680): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-09 15:22:48.634: E/AndroidRuntime(680): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-09 15:22:48.634: E/AndroidRuntime(680): at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-09 15:22:48.634: E/AndroidRuntime(680): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-09 15:22:48.634: E/AndroidRuntime(680): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 15:22:48.634: E/AndroidRuntime(680): at android.os.Looper.loop(Looper.java:137)
10-09 15:22:48.634: E/AndroidRuntime(680): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-09 15:22:48.634: E/AndroidRuntime(680): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 15:22:48.634: E/AndroidRuntime(680): at java.lang.reflect.Method.invoke(Method.java:511)
10-09 15:22:48.634: E/AndroidRuntime(680): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-09 15:22:48.634: E/AndroidRuntime(680): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-09 15:22:48.634: E/AndroidRuntime(680): at dalvik.system.NativeStart.main(Native Method)
10-09 15:22:48.634: E/AndroidRuntime(680): Caused by: java.lang.IllegalArgumentException: column 'name' does not exist
10-09 15:22:48.634: E/AndroidRuntime(680): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:267)
10-09 15:22:48.634: E/AndroidRuntime(680): at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78)
10-09 15:22:48.634: E/AndroidRuntime(680): at com.example.importcontacts.ImportContactsActivity.onCreate(ImportContactsActivity.java:46)
10-09 15:22:48.634: E/AndroidRuntime(680): at android.app.Activity.performCreate(Activity.java:4465)
10-09 15:22:48.634: E/AndroidRuntime(680): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-09 15:22:48.634: E/AndroidRuntime(680): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-09 15:22:48.634: E/AndroidRuntime(680): ... 11 more
答案 0 :(得分:2)
使用ContactsContract.CommonDataKinds.Phone.CONTENT_URI
代替ContactsContract.Contacts.CONTENT_URI
,以便您可以获取电话号码,并更改这些行
String name = cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(People.NUMBER));
String email = cursor.getString(cursor.getColumnIndexOrThrow(People.PRIMARY_EMAIL_ID));
到
String name = cursor.getString(cursor.getColumnIndexOrThrow(Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER));
您需要另一个查询来获取email_id。从第一个光标,您可以获得联系人ID,如此
long contactId = cursor.getLong(cursor.getColumnIndexOrThrow(Phone.CONTACT_ID));
现在使用过滤器中的contactId
查询电子邮件uri
long emailId;
Cursor cur = getContentResolver()
.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{"_id"},
"contact_id = " + contactId,
null,null);
if(cur != null && cur.moveToFirst()){
emailId = cur.getLong(0);
}