天天看点

android2.2根据电话号码查询联系人姓名的方法

[java] view

plaincopy

public class androidtest extends activity {  

    private static final string tag = "androidtest";  

    private textview m_textview;  

    private edittext m_edittext;  

    private string mnumber;  

    @override  

    public void oncreate(bundle savedinstancestate){  

        super.oncreate(savedinstancestate);  

        setcontentview(r.layout.main);  

        m_textview = (textview) findviewbyid(r.id.textview01);  

        m_edittext = (edittext) findviewbyid(r.id.edittext01);  

        m_textview.settextsize(20);  

        /** 

        * 设置当m_edittext中为空时提示的内容   

        * 在xml中同样可以实现:android:hint="请输入账号" 

        */  

        m_edittext.sethint("请输入账号");  

        /* 设置edittext事件监听 */  

        m_edittext.setonkeylistener(new edittext.onkeylistener() {  

        public boolean onkey(view arg0, int arg1, keyevent arg2)  

        {  

            // todo auto-generated method stub  

            // 得到文字,将其显示到textview中  

//          m_textview.settext("文本框中内容是:" + m_edittext.gettext().tostring());  

            return false;  

        }  

        });  

        m_edittext.setonfocuschangelistener(new onfocuschangelistener() {  

            public void onfocuschange(view v, boolean hasfocus) {  

                // todo auto-generated method stub  

                mnumber = ((edittext)v).gettext().tostring();  

                log.d(tag, "mnumber = " + mnumber);  

                getpeople();  

            }  

        m_edittext.addtextchangedlistener(new textwatcher() {  

            public void aftertextchanged(editable s) {  

            public void beforetextchanged(charsequence s, int start, int count,  

                    int after) {  

            public void ontextchanged(charsequence s, int start, int before,  

                    int count) {  

                // 得到文字,将其显示到textview中  

                m_textview.settext("文本框中内容是:" + m_edittext.gettext().tostring());  

    }  

    /* 

     * 根据电话号码取得联系人姓名 

     */  

    public void getpeople() {  

        string[] projection = { contactscontract.phonelookup.display_name,  

                                contactscontract.commondatakinds.phone.number};  

        log.d(tag, "getpeople ---------");  

        // 将自己添加到 mspeers 中  

        cursor cursor = this.getcontentresolver().query(  

                contactscontract.commondatakinds.phone.content_uri,  

                projection,    // which columns to return.  

                contactscontract.commondatakinds.phone.number + " = '" + mnumber + "'", // where clause.  

                null,          // where clause value substitution  

                null);   // sort order.  

        if( cursor == null ) {  

            log.d(tag, "getpeople null");  

            return;  

        log.d(tag, "getpeople cursor.getcount() = " + cursor.getcount());  

        for( int i = 0; i < cursor.getcount(); i++ )  

            cursor.movetoposition(i);  

            // 取得联系人名字  

            int namefieldcolumnindex = cursor.getcolumnindex(contactscontract.phonelookup.display_name);     

            string name = cursor.getstring(namefieldcolumnindex);  

            log.i("contacts", "" + name + " .... " + namefieldcolumnindex); // 这里提示 force close  

            m_textview.settext("联系人姓名:" + name);  

}

继续阅读