我正在尝试访问联系人的详细信息,但我只获得主要详细信息,我使用的是 Ecliar 平台,这意味着,
现在我使用此代码获取所有联系人的姓名、号码和电子邮件ID。但如果一个联系人有 2 个号码和 2 个电子邮件 ID,我只得到主号码和电子邮件 ID
我正在使用 ListView 中的显示,例如
1.姓名1,号码1,电子邮件ID 1。
2.Name 1, Number2, email Id 2. 第一人称,依此类推
3.Name 2, number 1, email Id 1 for second person 依此类推。将继续这样。
但我只得到特定联系人的 number1 和电子邮件 ID 1。没有得到该联系人的号码 2 和电子邮件 id2。
我编辑了循环逻辑,但出现了其他一些错误。如何解决这个问题呢 ?
代码
public class Details extends Activity {
ListView lvItem;
private Button btnAdd;
String emailAddress = "" ;
String displayName = "" ;
String 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.listview);
btnAdd = (Button)this.findViewById(R.id.btn);
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));
Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
while (emails.moveToNext())
{
emailAddress.add(emails.getString(emails.getColumnIndex(Email.DATA)));
break;
}
emails.close();
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)));
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 + " , " + phoneNumber.get(i) + " , " + emailAddress.get(i)+"\n");
}
else
{
contactlist.add(displayName + " , " + phoneNumber.get(i)+"\n");
}
}
}
else
{
for (int i=0; i<=emailCount; i++)
{
if(phoneNumberCount>i)
{
contactlist.add(displayName+", "+phoneNumber.get(i)+", "+ emailAddress.get(i)+"\n");
}
else
{
contactlist.add(displayName+","+ emailAddress.get(i)+"\n");
}
}
}
}
Collections.sort(contactlist);
itemAdapter.notifyDataSetChanged();
cursor.close();
}
}
任何解决方案?
日志猫
06-19 11:28:08.058: W/KeyCharacterMap(265): No keyboard for id 0
06-19 11:28:08.068: W/KeyCharacterMap(265): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-19 11:28:33.187: D/dalvikvm(265): GREF has increased to 201
06-19 11:33:32.118: W/KeyCharacterMap(307): No keyboard for id 0
06-19 11:33:32.118: W/KeyCharacterMap(307): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-19 14:16:35.488: D/AndroidRuntime(385): Shutting down VM
06-19 14:16:35.498: W/dalvikvm(385): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-19 14:16:35.498: E/AndroidRuntime(385): Uncaught handler: thread main exiting due to uncaught exception
06-19 14:16:35.507: E/AndroidRuntime(385): java.lang.IndexOutOfBoundsException: Invalid location 1, size is 1
06-19 14:16:35.507: E/AndroidRuntime(385): at java.util.ArrayList.get(ArrayList.java:341)
06-19 14:16:35.507: E/AndroidRuntime(385): at com.abhi.Trial3.readContacts(Trial3.java:100)
06-19 14:16:35.507: E/AndroidRuntime(385): at com.abhi.Trial3.access$0(Trial3.java:44)
06-19 14:16:35.507: E/AndroidRuntime(385): at com.abhi.Trial3$1.onClick(Trial3.java:38)
06-19 14:16:35.507: E/AndroidRuntime(385): at android.view.View.performClick(View.java:2364)
06-19 14:16:35.507: E/AndroidRuntime(385): at android.view.View.onTouchEvent(View.java:4179)
06-19 14:16:35.507: E/AndroidRuntime(385): at android.widget.TextView.onTouchEvent(TextView.java:6541)