android – 联系人选择器,检索姓氏,名字,电话号码

android – 联系人选择器,检索姓氏,名字,电话号码,第1张

概述现在,我可以检索电话号码并将editText的文本设置为该号码.但是,当我尝试获取姓氏或名字时,它不起作用.请注意我评论的内容. 继承我的代码: import android.app.Activity;import android.content.Intent;import android.database.Cursor;import android.net.Uri;import andr 现在,我可以检索电话号码并将editText的文本设置为该号码.但是,当我尝试获取姓氏或名字时,它不起作用.请注意我评论的内容.

继承我的代码:

import androID.app.Activity;import androID.content.Intent;import androID.database.Cursor;import androID.net.Uri;import androID.os.Bundle;import androID.provIDer.ContactsContract;import androID.provIDer.ContactsContract.CommonDataKinds.Phone;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;public class main extends Activity {    private static final int CONTACT_PICKER_RESulT = 1001;     /** Called when the activity is first created. */    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        button getContacts = (button)findVIEwByID(R.ID.getContacts);        getContacts.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                Intent i = new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);                startActivityForResult(i,CONTACT_PICKER_RESulT);            }        });    }    protected voID onActivityResult(int reqCode,int resultCode,Intent data) {        super.onActivityResult(reqCode,resultCode,data);        if(resultCode == RESulT_OK) {            switch (reqCode) {            case CONTACT_PICKER_RESulT:                Cursor cursor = null;                String number = "";                String lastname ="";                try {                    Uri result = data.getData();                    //get the ID from the uri                    String ID = result.getLastPathSegment();                      //query                    cursor = getContentResolver().query(                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone._ID + " = ? ",new String[] {ID},null);//                  cursor = getContentResolver().query(Phone.CONTENT_URI,//                          null,Phone.CONTACT_ID + "=?",new String[] { ID },//                          null);                    int numberIDx = cursor.getColumnIndex(Phone.DATA);                      if(cursor.movetoFirst()) {                        number = cursor.getString(numberIDx);                        //lastname = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Structuredname.FAMILY_name));                    } else {                        //WE Failed                    }                } catch (Exception e) {                    //Failed                } finally {                    if (cursor!=null) {                        cursor.close();                    }                    EditText numberEditText = (EditText)findVIEwByID(R.ID.number);                    numberEditText.setText(number);                    //EditText lastnameEditText = (EditText)findVIEwByID(R.ID.last_name);                    //lastnameEditText.setText(lastname);                }            }        }
解决方法 这就是我得到显示名称的方式……

@OverrIDeprotected voID onActivityResult(int requestCode,Intent data) {    if (resultCode == RESulT_OK) {        switch (requestCode) {        case CONTACT_PICKER_RESulT:            Cursor cursor = null;            try {                Uri result = data.getData();                String ID = result.getLastPathSegment();                //Get name                cursor = getContentResolver().query(result,null);                if (cursor.movetoFirst()) {                    name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.disPLAY_name));                } catch (Exception e) { }        }    }}

希望能帮助到你 :)

总结

以上是内存溢出为你收集整理的android – 联系人选择器,检索姓氏,名字,电话号码全部内容,希望文章能够帮你解决android – 联系人选择器,检索姓氏,名字,电话号码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1129557.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存