我正在尝试访问模拟器中所有联系人的联系人详细信息。我得到了访问其他详细信息所需的解决方案。访问电话和电子邮件类型时出现问题。我使用下面的代码来获取电话类型和电子邮件类型。应用程序正在运行,没有编译错误。
完整代码
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 正确显示。有人知道如何调试吗?