我想从联系人列表中选择联系人,并提取联系人姓名以在我的编辑文本中显示。 xml文件的代码如下:
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/display_name"
android:inputType="textPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="78dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/do_name_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="doLaunchContactPicker"
android:text="Button" />
MainActivity java文件的代码如下:
public class MainActivity extends Activity {
EditText e;
private static final int CONTACT_PICKER_RESULT = 1;
private static final String DEBUG_TAG = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doLaunchContactPicker(View view) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String name = "";
try {
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: "
+ result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for name
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { id },
null);
int phoneIdx = cursor.getColumnIndex(Phone.DATA);
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to get name", e);
} finally {
if (cursor != null) {
cursor.close();
}
e = (EditText) findViewById(R.id.display_name);
e.setText(name);
if (name.length() == 0) {
Toast.makeText(this, "Name not found for contact.",
Toast.LENGTH_LONG).show();
}
}
break;
}
} else {
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
导入声明是:
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
我还在清单文件中添加了“读取联系人”权限。
我可以从联系人列表中选择一个联系人,但名称没有出现在编辑文本中,而是我收到toast消息“找不到联系人姓名”。 提取电子邮件时也会出现同样的问题。请帮我。提前谢谢。
答案 0 :(得分:0)
您没有为name
变量设置值。您只是用空字符串初始化它。
删除行int phoneIdx = cursor.getColumnIndex(Phone.DATA);
并添加此代码
if (cursor != null && cursor.moveToFirst())
{
int phoneIdx = cursor.getColumnIndex(Phone.DATA);
int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
name = cursor.getString(nameIdx);
}
行后
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { id },
null);
答案 1 :(得分:0)
我刚刚更改了代码:int PhoneIdx = cursor.getColumnIndex(Phone.DATA);
到int PhoneIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
并且有效。