显示联系人姓名(重复姓名取决于联系人号码存在的名称)

问题描述:

我想在列表中显示“联系人姓名”和“联系人号码”。但是我的代码重复“联系人姓名”取决于与该特定姓名关联的数量或其他属性(即电子邮件ID)。例如 。 在联系人目录联系人列表中.. Pankaj Kumar和数字000000-000和00000-2222。我只想输出Pankaj Kumar和primary_number,但输出(Pankaj kumar和数字000000-000)和重复(Pankaj kumar和编号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); 

} 

如果你把你的查询如下

 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

嗨HellBoy,我打算使用你的代码,并且我有点混淆,如果我想使用TYPE_WORK的数字。那我怎么读这个数字? – 2011-01-18 05:55:31

+0

很简单。我编辑了我的答案。 – Vivek 2011-01-19 14:51:15