从联系人列表中选择姓名,并在android中显示编辑文本
我想从联系人列表中选择一个联系人,并提取联系人的姓名以在编辑文本中显示。xml文件的代码如下所示: activity_main.xml从联系人列表中选择姓名,并在android中显示编辑文本,android,android-edittext,android-contacts,Android,Android Edittext,Android Contacts,我想从联系人列表中选择一个联系人,并提取联系人的姓名以在编辑文本中显示。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
<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" />
进口声明如下:
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
我还在清单文件中添加了“读取联系人”权限
我可以从联系人列表中选择一个联系人,但姓名不会出现在编辑文本中,相反,我得到的是未找到联系人的toast消息名称。
同样的问题也发生在获取电子邮件时。请帮帮我。提前感谢。您没有设置name变量的值。您仅使用空字符串初始化它 删除int phoneIdx=cursor.getColumnIndexPhone.DATA行;并添加此代码
if (cursor != null && cursor.moveToFirst())
{
int phoneIdx = cursor.getColumnIndex(Phone.DATA);
int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
name = cursor.getString(nameIdx);
}
排队后
游标=getContentResolver.queryPhone.CONTENT\u URI,
null,Phone.CONTACT_ID+=?,新字符串[]{ID},
无效 我刚刚将代码更改为:int PhoneIdx=cursor.getColumnIndexPhone.DATA 到int PhoneIdx=cursor.getColumnIndexPhone.DISPLAY\u NAME
它成功了。上述解决方案没有成功。我仍然收到同样的祝酒词。名称未显示在edittext字段中。只需调试并查看“name”变量中是否有任何值已检查LogCat。显示Log语句中无法获取名称的消息。此外,还请求了大小为1的CursorIndexOutOfBoundsException:Index=-1。代码似乎很好,但错误在哪里呢。
if (cursor != null && cursor.moveToFirst())
{
int phoneIdx = cursor.getColumnIndex(Phone.DATA);
int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
name = cursor.getString(nameIdx);
}