如何通过联系人中的名称获取数字不适用于多字符串

时间:2012-02-28 13:59:49

标签: android

我试过这些链接 this one

但我仍然遇到错误并且工作很奇怪。我发布了我的代码,所以请帮助我。 我对在查询中传递的参数感到困惑。

我正在使用以下方法

void get_Number(String name)

    {

        //String name = "Daddy";

        String number = null;
            String args[]= {name};


        ContentResolver cr = getContentResolver();

        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                "DISPLAY_NAME = ?", args, null);

        if (cursor.moveToFirst()) {

            String contactId = cursor.getString(cursor
                    .getColumnIndex(BaseColumns._ID));

            Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID
                    + " = " + contactId, null, null);
            while (phones.moveToNext()) {

                number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
                int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
                Log.v("TAG3", number);

            }
        }


the problem is in the line 

Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,null,                     “DISPLAY_NAME =?”,args,null);

上述方法适用于单字符串,但不适用于多字符串。 如何修改它以正确使用联系人中的多个单词名称。

the otherway i have tried is



try

        {

            ContentResolver cr = getContentResolver();

            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);

            if (cur.getCount() > 0) 

            {

                while (cur.moveToNext()) 

                {

                    //String iur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

                    String Name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
                    {
                        if(Name.equalsIgnoreCase(name))
                        {
                            Cursor tempc=

                            etContentResolver().query(ContactsContract.CommonDataKinds.
                             Phone.CONTENT_URI,new String[]

                             {ContactsContract.CommonDataKinds.Phone.NUMBER},
                             ContactsContract.CommonDataKinds.Phone._ID+ 
                               "="+cur.getString(cur.getColumnIndex
                             (ContactsContract.Contacts._ID)), null,null);

                          tempc.moveToFirst();

                        //retrieve the phone number

                        number = 
                               tempc.getString(cur.getColumnIndex
                               (ContactsContract.PhoneLookup.NUMBER)); 



                        }
                    }
                }
            }
        }
        catch (Exception e) 
        {
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
        }

        if(number!=null)
            txt.append(number + "\n");
       else
        txt.append("No name");

问题在于嵌套在if(Name.equalsIgnoreCase(name))子句中的代码。 我已经尝试了各种方法从光标变量中检索数字,但它给我一个错误。所以如果有联系人的姓名匹配,请告诉我检索号码的正确方法。提前谢谢。

有人请帮助我在过去2天尝试...

这是logcat的输出

02-29 16:56:04.458:E / CursorWindow(1197):字段0,-1的错误请求。 numRows = 1,numColumns = 1

2 个答案:

答案 0 :(得分:0)

在第二个代码段中,更改以下行:

...
    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
    {
        if(Name.equalsIgnoreCase(name))
        {
             Cursor tempc=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone._ID+"="+cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)), null,null);
             tempc.moveToFirst();
             //retrieve the phone number
             number = tempc.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.NUMBER)); 
        }
   }
   else
         continue;
...

编辑:

正如你在问题中所说的,这条线路的错误是错误的:

Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                "DISPLAY_NAME = ?", args, null);    

然后将此行更改为低于1并尝试:

Cursor cursor=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like '"+args+"'", null,null);

编辑 - 1:

Cursor tempc=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)), null,null);

答案 1 :(得分:0)

void get_Number(String name) 
{       
        String number = null;

        String[] args= {name};
        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
        "DISPLAY_NAME = ?", args, null);

            if (cursor.moveToFirst()) {

            String contactId = cursor.getString(cursor
                    .getColumnIndex(BaseColumns._ID));    
            Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID
                    + " = " + contactId, null, null);

            while (phones.moveToNext()) {

                number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
                int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
                Log.v("TAG3", number);
            }
        }    
        if (number != null)
            txt.append(number + "\n");
        else
            txt.append("No name");    
  }

或者

String where= "DISPLAY_NAME like ?";            
Cursor  people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,    
 null, where, new String[]{name}, null);

people.moveToFirst();  

try{
        String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
        String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
        if ( hasPhone.equalsIgnoreCase("1"))
              hasPhone = "true";
        else
              hasPhone = "false" ;
        if (Boolean.parseBoolean(hasPhone)) 
        {
              Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
              while (phones.moveToNext()) 
              {
                   number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                   // mConno.add(position,phoneNumber);    
              }
              phones.close(); 
        }               
}       
catch(Exception e){ 
}

if(number!=null)
     txt.append(number + "\n");
else
     txt.append("No name");

它不适用于多字符串的原因是必须将整个名称保存为名字,不要将其拆分为名字和姓氏。然后,上述方法中的任何一种都适用于你!