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

时间:2011-01-05 06:36:43

标签: android

我想在列表中显示“联系人姓名”和“联系电话号码”。但我的代码重复“联系人姓名”取决于与该特定名称相关联的数量或其他属性(即电子邮件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);

}

1 个答案:

答案 0 :(得分: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;
                }
            }
        }
    }

您的联系人姓名不会重复相同的号码,并选择您想要的号码 把移动,工作,其他或家庭作为主要形式。