显示姓名和联系电话在我自己的列表视图使用android 2.1



我试图在我自己的列表视图中检索所有联系人姓名和他们的号码。我实现了获得所有的名字,但是当我试图获得电话号码时,它将在每个联系人上显示相同的号码。

其中number从HAS_PHONE_NUMBER获取值1

我的代码是

if (number > 0) {
            Cursor phones = managedQuery(
                         ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                         ContactsContract.CommonDataKinds.Phone.CONTACT_ID , 
                         null, null);
startManagingCursor(phones);
             phones.moveToFirst();  
       String cNumber = phones.phones.getString(phones.getColumnIndex("data1"));  
       cache.nameView.setText(cache.nameBuffer.data, 0, size);
     cache.numView.setText(cNumber);

}

Thanks in advance.

试试这个:

        //get all contacts
        Cursor peopleCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null,null, null);
        if(peopleCursor.getCount()>0)
        {  
            peopleCursor.moveToFirst();
            for(int i=0;i<peopleCursor.getCount();i++)
            {  
               if(check for HAS_PHONE_NUMBER)
               {
                   //get number
                   Cursor numberCursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},ContactsContract.CommonDataKinds.Phone._ID+"="+peopleCursor.getString(peopleCursor.getColumnIndex(ContactsContract.Contacts._ID)), null,null);
                   numberCursor.moveToFirst(); 
                   String number=numberCursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                   //get name
                   String name=peopleCursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                   peopleCursor.moveToNext();                   
               }
            }
       }      

必须设置While或for循环。在for代码中,你使用if基本上它用于不能增加number变量值的条件。count读取名称的总数它的变量名totalNumber_name

if (number == totalNumber_name) {
        Cursor phones = managedQuery(
                     ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                     ContactsContract.CommonDataKinds.Phone.CONTACT_ID , 
                     null, null);
startManagingCursor(phones);
         phones.moveToFirst();  
   String cNumber = phones.phones.getString(phones.getColumnIndex("data1"));  
   cache.nameView.setText(cache.nameBuffer.data, 0, size);
 cache.numView.setText(cNumber);
number++;
}

可能是它的工作

试试这段代码,它在我的应用程序中工作得很好。

while (c.moveToNext())

    {
        contactName = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
        if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactID },null);
            while (pCur.moveToNext()) {
                contactTelNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            } 
        }
          Log.i("name ", contactName + " ");
        Log.i("number ", contactTelNumber + " ");

最新更新