【问题标题】:display name and contact number in my own list view using android 2.1使用 android 2.1 在我自己的列表视图中显示姓名和联系电话
【发布时间】:2011-12-20 22:17:16
【问题描述】:

我正在尝试在我自己的列表视图中检索所有联系人姓名及其号码。我很想得到所有的名字,但是当我也想得到电话号码时,它会在每个联系人上向我显示相同的号码。

其中 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);

}

提前谢谢..

【问题讨论】:

    标签: android android-listview android-contacts


    【解决方案1】:

    试试这个:

            //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();                   
                   }
                }
           }      
    

    【讨论】:

      【解决方案2】:

      你必须设置 While 或 for 循环。在你使用的代码中,如果基本上它用于不能增加你的数字变量值的条件。 统计你提取的姓名总数 它是变量名 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++;
      }
      

      可能是工作

      【讨论】:

        【解决方案3】:

        试试这个代码,它在我的应用程序中运行良好。

        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 + " ");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-25
          相关资源
          最近更新 更多