联系方式来了不正确的订单

时间:2012-06-28 11:31:19

标签: android android-contacts

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

完整代码

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个号码 [Home,Mobile] 和2个电子邮件 [Home,Work] 保存到特定联系人。当我运行代码时,此联系人的ListView显示变为数字 [移动,工作] 和电子邮件 [移动,工作] 。我无法更正代码。如何正确显示ListView.Anybody知道如何调试它?

1 个答案:

答案 0 :(得分:0)

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

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

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

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

修改

我认为这就是你想要的:

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