2 alee love alee_love 于 2013.05.12 22:43 提问

Android根据姓名来查找电话问题

大家好!刚学Android,刚写了一个根据姓名来查找电话的小程序,有如下问题,希望各位指导一下,谢谢,麻烦说的详细一点,再次感谢。
问题(logcat):

1、Can't open keycharmap file
2、Error loading keycharmap file '/system/usr/keychars/cyttsp-i2c.kcm.bin'. hw.keyboards.65542.devname='cyttsp-i2c'
3、Using default keymap: /system/usr/keychars/qwerty.kcm.bin

代码:

public class MainActivity extends Activity {
    private Button b1;
    private EditText myEditText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         b1 = (Button)findViewById(R.id.Button01);

         myEditText = (EditText)findViewById(R.id.EditText01);
         b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 String name = myEditText.getText().toString();
                 Bundle data = new Bundle();
                 data.putString("name", name);

                 Intent intent = new Intent(MainActivity.this,ResultActivity.class);
                 intent.putExtras(data);
                 startActivity(intent);
            }
        });

    }
}


public class ResultActivity extends Activity {
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        //String id;
        //ContentResolver contentResolver = this.getContentResolver();
        //Cursor cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,
        //  new String[]{android.provider.ContactsContract.Contacts._ID}, null, null, null);
                //while(cursor.moveToNext()) {
        //  id=cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID)); 
        tv = (TextView)findViewById(R.id.TextView02);
        Intent intent = getIntent();
        Bundle b = intent.getExtras();
        String uName = b.getString("name");



        boolean isQueryAll = false; 
        // cu姓名游标,cn电话号码游标 
        Cursor cu, cn = null; 
        // 查询条件,SQL是的Where语句的后部分 
        String selection = null; 

        Uri contacts =ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        Uri con= ContactsContract.Contacts.CONTENT_URI;
        uName = uName.trim(); 
        // 是否查询全部通讯录,如果姓名为空则是 
        isQueryAll = uName.equals("") ? true : false; 

        if (isQueryAll) { 
            // 查询全部时的,查询条件,主要用在cu游标上 
            selection = ContactsContract.Data.MIMETYPE 
                    + "='"
                    + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE 
                    + "'"; 
            //System.out.println("Query For ALl--" + selection); 
        } else { 
            // 根据姓名查询时的,查询条件,主要用在cu游标上 
            selection = ContactsContract.Data.MIMETYPE 
                    + "='"
                    + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE 
                    + "'"
                    + " AND "
                    + ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME 
                    + " LIKE " + "'%" + uName + "%'"; 
            //System.out.println("Query For Some--" + selection); 
        } 

        try { 
            // 根据姓名查询出完整姓名和通讯录ID 

            cu = getContentResolver()
                    .query( 
                            con, 
                            new String[] { 
                                    ContactsContract.Data.RAW_CONTACT_ID, 
                                    ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME }, 
                            selection, null, null); 
            // 根据通讯录ID,查找对应的电话号码的查询条件,主要用于cn游标 
            selection = ContactsContract.Data.MIMETYPE + "='"
                    + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE 
                    + "'" 
                    + " AND " 
                    + ContactsContract.Data.RAW_CONTACT_ID 
                    + "=?"; 
            //System.out.println("Number Query--" + selection); 
            while (cu.moveToNext()) { 
                String contactId = String.valueOf(cu.getInt(0)); 
                // 开始查找电话号码 
                //System.out.println("  Start Query Num"); 
                cn = getContentResolver() 
                        .query( 
                                 contacts, 
                                new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER }, 
                                selection, new String[] { contactId }, null); 

                while (cn.moveToNext()) { 
                    // 将一组通讯录记录在HashMap中 
                   // HashMap<String, String> map = new HashMap<String, String>(); 
                   // map.put("display_name", cu.getString(1)); 
                    //map.put("phone_number", cn.getString(0)); 
                    // 将查到通讯录添加到List中 
                   // list.add(map); 
                    String name1 = cu.getString(1);
                    String number = cn.getString(0);
                    tv.setText(name1+":"+number);
                } 
            } 
            //关闭游标 
            cu.close(); 
            cn.close(); 
        } catch (Exception e) { 
            // TODO: handle exception 
        } 
        //String[] projection = new String[] { People._ID,
        //      People.NAME, People.NUMBER };
        //String[] projection = new String[] { ContactsContract.Contacts._ID,
        //      ContactsContract.Contacts.DISPLAY_NAME };

        //Uri contacts = People.CONTENT_URI;
        //Uri contacts =Phone.CONTENT_URI;
        //Uri contacts =ContactsContract.Data.CONTENT_URI;
         //Uri contacts = ContactsContract.Contacts.CONTENT_URI;
        //Uri contacts =ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        //  String[] args = {name};

        //Cursor managedCursor = managedQuery(contacts, projection,                                                                         
            //  "name=?", 
                //args, 
        //      People.NAME + " ASC");
                //ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        //Uri contactUri = ContactsContract.Contacts.CONTENT_URI;

      //  String[] PROJECTION = new String[] {

           //     ContactsContract.Contacts._ID,

           //     ContactsContract.Contacts.DISPLAY_NAME,

            //    ContactsContract.Contacts.HAS_PHONE_NUMBER

    //    };

    //String SELECTION = ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'";
    //String SELECTION = ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'";

        //Cursor managedCursor = managedQuery(contacts, projection, "name=?", args,
        //ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
            //Cursor phoneCursor = contentResolver.query(
            //  android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            //  null, android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+id, null, null);

            //if(managedCursor.moveToFirst()){
            //  String name1 = managedCursor.getString(1);
            //  String number = managedCursor.getString(2);
            //  tv.setText(name1+":"+number);
            //}

    }
}

4个回答

zhangzhen199121
zhangzhen199121   2013.05.12 23:09

现在正在学习 过来顶一个 不错哦!继续努力加油!

suannai0314
suannai0314 下次您这样的回答可以直接在问题下边添加评论。
4 年多之前 回复
o19830015
o19830015   2013.05.13 09:59

我遇到过这样的问题

Android:label的值必须是字符串资源,不可以是硬编码字符串。

u010673610
u010673610   2013.05.13 10:23

路过!学理的就是好呀

suannai0314
suannai0314 下次您这样的回答可以直接在问题下边添加评论。
4 年多之前 回复
u010675041
u010675041   2013.05.13 11:40

真心厉害,软件高手,向你致敬!

suannai0314
suannai0314 下次您这样的回答可以直接在问题下边添加评论。
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!