1

我正在尝试访问模拟器中所有联系人的联系人详细信息。我得到了访问其他详细信息所需的解决方案。访问电话和电子邮件类型时出现问题。我使用下面的代码来获取电话类型和电子邮件类型。应用程序正在运行,没有编译错误。

完整代码

public class GetAllDatas extends Activity {

ListView lvItem;
private Button btnAdd;
String Ptype, Etype;
int contactPhoneType;

String displayName="", emailAddress="", phoneNumber="";
ArrayList<String> contactlist=new ArrayList<String>(); 
ArrayAdapter<String> itemAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lvItem = (ListView)this.findViewById(R.id.lvitems); 
    btnAdd = (Button)this.findViewById(R.id.btnAddItem);

    itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist); 
    lvItem.setAdapter(itemAdapter);

    btnAdd.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) {
            readContacts();
        }
    });

} 

private void readContacts()
{
    ContentResolver cr =getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);


    while (cursor.moveToNext()) 
    {
        displayName  = "" ;
        ArrayList<String> phoneNumber  = new ArrayList<String>();
        ArrayList<String> emailAddress  = new ArrayList<String>();

        displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));       
        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

        /*                   Email                   */ 

        Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
        while (emails.moveToNext()) 
        {
            emailAddress.add(emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 

             int contactPhoneType = emails.getInt(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));



             switch(contactPhoneType){
                case ContactsContract.CommonDataKinds.Email.TYPE_HOME: 
                     Etype = "Home";
                     break; 
                case ContactsContract.CommonDataKinds.Email.TYPE_WORK: 
                    Etype = "Work";
                    break;
                case ContactsContract.CommonDataKinds.Email.TYPE_MOBILE: 
                    Etype = "Mobile";

                    break;
                case ContactsContract.CommonDataKinds.Email.TYPE_OTHER: 
                    Etype = "Other";
                    break;
           }                  
       }



        emails.close(); 


      /*            Phone Number and Type   */ 

        if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
        {
            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
            while (pCur.moveToNext()) 
            {
                 phoneNumber.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 

                  contactPhoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));


                 switch(contactPhoneType){
                    case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: 
                         Ptype = "Home";
                         break; 
                    case ContactsContract.CommonDataKinds.Phone.TYPE_WORK: 
                        Ptype = "Work";
                        break; 
                    case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:  
                        Ptype = "Mobile";
                        break; 
                    case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER: 
                        Ptype = "Other";
                        break;
               }                  
           }  



            pCur.close();  
        }   

int phoneNumberCount = phoneNumber.size();
int emailCount = emailAddress.size();

// Add arraylist data to contactlist..

if(phoneNumberCount > emailCount) {

    for (int i=0; i<phoneNumberCount; i++)
      {
        if(emailCount>i)
          {
           contactlist.add(displayName +  " , " +  Ptype + " , " + phoneNumber.get(i) + " , " + Etype + " , " + emailAddress.get(i) + "\n");    
           }
        else
        {
          contactlist.add(displayName + " , " +  Ptype + " , " + phoneNumber.get(i) + " , " + " , " + "\n");
        } 
       }
    }
    else  
       {
        for (int i=0; i<emailCount; i++) 
         {
        if(phoneNumberCount>i)
          {
            contactlist.add(displayName + " , " + Ptype + " , " + phoneNumber.get(i) + " , " +  Etype + " , " + emailAddress.get(i) + "\n"); 
          }
        else
        { 
          contactlist.add(displayName + " , " + " , " + " , " + Etype + " , " + emailAddress.get(i) +  "\n");
        }
      }
    }      
    }
    Collections.sort(contactlist);
    itemAdapter.notifyDataSetChanged();
    cursor.close(); 
} 
 }

问题是,我在不正确顺序的 ListView 中得到了电话类型和电子邮件类型。这意味着我将 2 个号码[家庭、手机]和 2 封电子邮件[家庭、工作]保存到特定联系人。当我运行代码时,此联系人的列表视图显示变为数字[手机、工作]和电子邮件[手机、工作] ] . 我无法更正代码。如何使 ListView 正确显示。有人知道如何调试吗?

4

1 回答 1

0

问题出在您的循环中...每个项目只有一个变量,并且每次都在重新设置它。

当您创建要添加到列表视图的字符串时,您只有通过每个循环的最后一次迭代的值。

您应该将大部分字符串创建移动到这些循环中。

在循环外开始字符串,设置联系人姓名,然后通过电话#循环在每次迭代中添加电话号码(和类型),然后通过 e 在每次迭代中将电子邮件(和类型)添加到字符串中- 邮件循环。

编辑

我认为这就是你想要的:

private void readContacts() {
    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);

    while (cursor.moveToNext()) {
        displayName = "";

        displayName = cursor
                .getString(cursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String id = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Contacts._ID));
        String holder = "";
        holder = holder + displayName;

        /* Phone Number and Type */

        if (Integer
                .parseInt(cursor.getString(cursor
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            + " = ?", new String[] { id }, null);
            while (pCur.moveToNext()) {

                contactPhoneType = pCur
                        .getInt(pCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

                switch (contactPhoneType) {
                case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                    holder = holder + ", Home";
                    break;
                case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                    holder = holder + ", Work";
                    break;
                case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                    holder = holder + ", Mobile";
                    break;
                case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
                    holder = holder + ", Other";
                    break;
                }
                holder = holder
                        + ", "
                        + (pCur.getString(pCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
            }
            pCur.close();
        }

        /* Email */
        Cursor emails = cr.query(Email.CONTENT_URI, null, Email.CONTACT_ID
                + " = " + id, null, null);
        while (emails.moveToNext()) {
            int contactPhoneType = emails
                    .getInt(emails
                            .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
            switch (contactPhoneType) {
            case ContactsContract.CommonDataKinds.Email.TYPE_HOME:
                holder = holder + ", Home";
                break;
            case ContactsContract.CommonDataKinds.Email.TYPE_WORK:
                holder = holder + ", Work";
                break;
            case ContactsContract.CommonDataKinds.Email.TYPE_MOBILE:
                holder = holder + ", Mobile";
                break;
            case ContactsContract.CommonDataKinds.Email.TYPE_OTHER:
                holder = holder + ", Other";
                break;
            }
            holder = holder
                    + ", "
                    + (emails
                            .getString(emails
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        }
        emails.close();
        contactlist.add(holder);
    }
    Collections.sort(contactlist);
    itemAdapter.notifyDataSetChanged();
    cursor.close();
}
于 2012-06-28T11:39:55.983 回答