2011-01-05 10 views
0

「連絡先の名前」と「連絡先の電話番号」をリストに表示します。しかし、私のコードは「連絡先名」を繰り返すのは、その特定の名前に関連付けられた数または他の属性(つまり電子メールID)に依存します。例えば ​​。 コンタクトディレクトリの連絡先リストに.. Pankaj Kumarと数字000000-000と00000-2222。私はPankaj Kumarとprimary_numberだけを出力しますが、出力は(Pankaj kumarとnumber 000000-000)とその繰り返しが(Pankaj kumarとnumber 00000-2222)となります。私はそれを解決するにはどうすればよい連絡先の名前を表示する(名前は連絡先の番号が名前に応じて変わります)

.. 私のコードは以下のように..です

 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);

// We'll define a custom screen layout here (the one shown above), but 
    // typically, you could just use the standard ListActivity layout. 
    setContentView(R.layout.contacts_list_item); 

    Cursor mCursor = getContentResolver().query(Data.CONTENT_URI, 
        null,      // projection 
        null,      // selection 
        null,      // selectionArgs 
        Data.DISPLAY_NAME);   // sortOrder   

    startManagingCursor(mCursor); 



    // Now create a new list adapter bound to the cursor. 
    // SimpleListAdapter is designed for binding to a Cursor. 
    contactAdapter = new SimpleCursorAdapter(
      this, // Context. 
      android.R.layout.two_line_list_item, // Specify the row template to use (here, two columns bound to the two retrieved cursor rows). 
      mCursor, // Pass in the cursor to bind to. 
      new String[] {Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},   // Array of cursor columns to bind to. 
      new int[] {android.R.id.text1, android.R.id.text2}); // Parallel array of which template objects to bind to those columns. 

    // Bind to our new adapter. 
    setListAdapter(contactAdapter); 

} 

答えて

1

あなたのクエリを置く場合

 Cursor cursor = getContentResolver(). 
    query(Contacts.CONTENT_URI, 
      new String[]{Contacts.DISPLAY_NAME}, null, null,null); 
    if(cursor!=null){ 
     while(cursor.moveToNext()){ 
      Cursor c = getContentResolver().query(Phone.CONTENT_URI, new String[]{Phone.NUMBER, Phone.TYPE}, 
        " DISPLAY_NAME = '"+cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME))+"'", null, null); 
      while(c.moveToNext()){ 
       switch(c.getInt(c.getColumnIndex(Phone.TYPE))){ 
       case Phone.TYPE_MOBILE :break; 
       case Phone.TYPE_HOME :break; 
       case Phone.TYPE_WORK : String workNo = c.getString(c.getColumnIndex(Phone.NUMBER));break; 
       case Phone.TYPE_OTHER :break; 
       } 
      } 
     } 
    } 

に従うよう、連絡先の名前が同じ数だけ繰り返して、選択しません。どちらの番号を にしたいかは、モバイル、仕事、その他、または家庭のプライマリフォームにします。

+0

こんにちはヘルボーイ、私はあなたのコードを使用するつもりです、そして、私はTYPE_WORKの番号を使用したい場合、少し混乱します。それでは、どうすればその番号を読むことができますか? –

+0

それは簡単です。私は自分の答えを編集しました。 – Vivek

関連する問題