2012-04-16 5 views
0

連絡先の名前から電話番号を取得したい。私は、このコードSQLiteの文字列でのクエリ

public String getPhoneNumber(String name) { 
    String phonenumber = ""; 
    ContentResolver cr = getContentResolver(); 
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
      "DISPLAY_NAME LIKE '%" + name + "%'", null, null); 
    if (cursor.moveToFirst()) { 
     String contactId = cursor.getString(cursor 
       .getColumnIndex(ContactsContract.Contacts._ID)); 
     // 
     // Get all phone numbers. 
     // 
     Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID 
       + " = " + contactId, null, null); 
     while (phones.moveToNext()) { 
      String number = phones.getString(phones 
        .getColumnIndex(Phone.NUMBER)); 
      int type = phones.getInt(phones.getColumnIndex(Phone.TYPE)); 
      switch (type) { 
      case Phone.TYPE_HOME: 
       //phonenumber = number; 
       // do something with the Home number here... 
       break; 
      case Phone.TYPE_MOBILE: 
       phonenumber = number; 
       // do something with the Mobile number here... 
       break; 
      case Phone.TYPE_WORK: 
       //phonenumber = number; 
       // do something with the Work number here... 
       break; 
      } 
     } 
     phones.close(); 
     // 
     // Get all email addresses. 
     // 

    } 
    cursor.close(); 
    return phonenumber; 
} 

を持っていますが、連絡先の名前は、DB内の名前と正確に一致していない場合、それは何も返さないでしょう。だから、たとえ入力の名前でさえも電話番号を取得する方法がないのであれば、それはあまりありませんか?

たとえば、連絡先があります:プロトタイプアレックスと私の入力名は:プロトタイプまたはアレックスです。

+0

'Prototype Alex 'LIKE'%prototype% ''は本当です(それが有効な構文だった場合) – zapl

答えて

0

これはあなたの具体的な問題に対する答えではありませんが、それは役に立つかもしれません。 Levenshtein距離、soundexまたはメタフォンのようなapproximate string matchingのアルゴリズムがあります。 コンテンツプロバイダからすべての連絡先を取得して、これらのアルゴリズムのいずれかを使用して検索を行うことは、あなたの選択肢かもしれません。

+0

ありがとうございました:D、私はこれを試してみましょう! –

+0

連絡先の名前が "Ân"(ユニコード文字)の場合、 "ân"または "Ân"でクエリを実行しようとすると、返されるデータはnullです。ただし、返されたデータは正しい場合です。 「好き」の代わりに「=」を使用しています。だから何をすべきか? –