尝试在我的Android应用程序中匹配相应的光标记录
在onCreate中,我有一个名为phones的光标:尝试在我的Android应用程序中匹配相应的光标记录,android,while-loop,android-contacts,android-cursor,Android,While Loop,Android Contacts,Android Cursor,在onCreate中,我有一个名为phones的光标: // this query only return contacts with phone number and is not duplicated phones = getContentResolver().query( // the table to query ContactsContract.Contacts.CONTENT_URI, //
// this query only return contacts with phone number and is not duplicated
phones = getContentResolver().query(
// the table to query
ContactsContract.Contacts.CONTENT_URI,
// the columns to return
null,
// selection criteria :
// we only want contacts that have a name and a phone number. If they have a phone number, the value is 1 (if not, it is 0)
ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'" + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1",
// selection criteria
null,
// display in ascending order
ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
// phones.getCount returns 134 records, which is the correct number of contacts with corresponding phone number.
我循环查找所有134个联系人的姓名:
if (phones != null) {
if (phones.getCount() == 0) {
Toast.makeText(MainActivity.this, "No contacts in your contact list.", Toast.LENGTH_LONG).show();
}
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
现在我需要得到134个匹配的电话号码。就在上面最后一行的下方,仍然在While循环中,我创建了一个新光标,phonestwo:
// phoneContactId returns 134 records, which is the correct number - it matches the number of contacts with corresponding phone numbers.
String phoneContactId = phones.getString(phones.getColumnIndexOrThrow(BaseColumns._ID));
phonestwo = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{phoneContactId},
null);
while (phonestwo.moveToNext()) {
int phoneType = phonestwo.getInt(phonestwo.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneNumber = phonestwo.getString(phonestwo.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
//For every contact in the phonebook, show the name and number
SelectContact selectContact = new SelectContact();
selectContact.setName(name);
selectContact.setPhone(phoneNumber);
selectContacts.add(selectContact);
}
}
}
phones.close();
return null;
}
在我的日志中
我有134个“电话”记录,这是正确的
我有134个“phoneContactId”记录,这是正确的
我有1张,有时2张“电话两张”记录。但我希望两条记录的电话号码为134,这是134个联系人的匹配电话号码。有什么想法吗
下面是我的日志的一个片段:
04-09 01:22:42.384 8934-8948/com.example.chris.contactlistcustomlistview E/phones﹕ 134
04-09 01:22:42.384 8934-8948/com.example.chris.contactlistcustomlistview E/phonestwo﹕ 2
04-09 01:22:42.384 8934-8948/com.example.chris.contactlistcustomlistview E/Contact ID﹕ 1191
04-09 01:22:42.394 8934-8934/com.example.chris.contactlistcustomlistview D/OpenGLRenderer﹕ Enabling debug mode 0
04-09 01:22:42.404 8934-8948/com.example.chris.contactlistcustomlistview E/phones﹕ 134
04-09 01:22:42.404 8934-8948/com.example.chris.contactlistcustomlistview E/phonestwo﹕ 2
04-09 01:22:42.404 8934-8948/com.example.chris.contactlistcustomlistview E/Contact ID﹕ 2314
04-09 01:22:42.414 8934-8948/com.example.chris.contactlistcustomlistview E/phones﹕ 134
04-09 01:22:42.414 8934-8948/com.example.chris.contactlistcustomlistview E/phonestwo﹕ 1
04-09 01:22:42.414 8934-8948/com.example.chris.contactlistcustomlistview E/Contact ID﹕ 1076
04-09 01:22:42.424 8934-8948/com.example.chris.contactlistcustomlistview E/phones﹕ 134
04-09 01:22:42.424 8934-8948/com.example.chris.contactlistcustomlistview E/phonestwo﹕ 1
04-09 01:22:42.424 8934-8948/com.example.chris.contactlistcustomlistview E/Contact ID﹕ 643
04-09 01:22:42.454 8934-8948/com.example.chris.contactlistcustomlistview E/phones﹕ 134
04-09 01:22:42.454 8934-8948/com.example.chris.contactlistcustomlistview E/phonestwo﹕ 1
04-09 01:22:42.454 8934-8948/com.example.chris.contactlistcustomlistview E/Contact ID﹕ 746
04-09 01:22:42.474 8934-8948/com.example.chris.contactlistcustomlistview E/phones﹕ 134
04-09 01:22:42.474 8934-8948/com.example.chris.contactlistcustomlistview E/phonestwo﹕ 1
04-09 01:22:42.474 8934-8948/com.example.chris.contactlistcustomlistview E/Contact ID﹕ 1690
04-09 01:22:42.504 8934-8948/com.example.chris.contactlistcustomlistview E/phones﹕ 134
04-09 01:22:42.504 8934-8948/com.example.chris.contactlistcustomlistview E/phonestwo﹕ 1
04-09 01:22:42.504 8934-8948/com.example.chris.contactlistcustomlistview E/Contact ID﹕ 1055
04-09 01:22:42.524 8934-8948/com.example.chris.contactlistcustomlistview E/phones﹕ 134
04-09 01:22:42.524 8934-8948/com.example.chris.contactlistcustomlistview E/phonestwo﹕ 1
04-09 01:22:42.524 8934-8948/com.example.chris.contactlistcustomlistview E/Contact ID﹕ 945
04-09 01:22:42.554 8934-8948/com.example.chris.contactlistcustomlistview E/phones﹕ 134
04-09 01:22:42.554 8934-8948/com.example.chris.contactlistcustomlistview E/phonestwo﹕ 2
04-09 01:22:42.554 8934-8948/com.example.chris.contactlistcustomlistview E/Contact ID﹕ 226
04-09 01:22:42.564 8934-8948/com.example.chris.contactlistcustomlistview E/phones﹕ 134
04-09 01:22:42.564 8934-8948/com.example.chris.contactlistcustomlistview E/phonestwo﹕ 1
04-09 01:22:42.564 8934-8948/com.example.chris.contactlistcustomlistview E/Contact ID﹕ 267
公共类MainActivity扩展活动{
光标;
ListView主ListView;
ArrayList hashMapsArrayList;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
如果(光标!=null){
cursor.moveToFirst();}
试一试{
游标=getApplicationContext().getContentResolver()
.query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,null,null,null,null);
int Idx=cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.CONTACT\u ID);
int nameIdx=cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY\u NAME);
int phoneNumberIdx=cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER);
int photoidix=cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.PHOTO\u缩略图\u URI);
cursor.moveToFirst();
Set id=new HashSet();
做{
System.out.println(“===>in-while”);
String contactid=cursor.getString(Idx);
如果(!ids.contains(contactid)){
添加(联系人ID);
HashMap HashMap=新的HashMap();
String name=cursor.getString(nameIdx);
String phoneNumber=cursor.getString(phoneNumberIdx);
字符串image=cursor.getString(photoIDIX);
System.out.println(“Id-->”+contactid+“Name-->”+Name);
System.out.println(“Id-->”+contactid+“Name-->”+Name);
System.out.println(“Id-->”+联系人Id+“号码-->”+电话号码);
如果(!phoneNumber.contains(“*”)){
hashMap.put(“contactid”,即“+contactid”);
hashMap.put(“名称”,“名称+名称”);
hashMap.put(“phoneNumber”,即“+phoneNumber”);
hashMap.put(“图像”,“图像+图像”);
//hashMap.put(“电子邮件”,“电子邮件+电子邮件”);
if(hashMapsArrayList!=null){
hashMapsArrayList.add(hashMap);}
//hashMapsArrayList.add(hashMap);
}
}
}while(cursor.moveToNext());
}捕获(例外e){
e、 printStackTrace();
}最后{
如果(光标!=null){
cursor.close();
}
}
}
}
public class MainActivity extends Activity {
Cursor cursor;
ListView mainListView;
ArrayList hashMapsArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (cursor != null) {
cursor.moveToFirst();}
try {
cursor = getApplicationContext().getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
int Idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int phoneNumberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int photoIdIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI);
cursor.moveToFirst();
Set<String> ids = new HashSet<>();
do {
System.out.println("=====>in while");
String contactid=cursor.getString(Idx);
if (!ids.contains(contactid)) {
ids.add(contactid);
HashMap<String, String> hashMap = new HashMap<String, String>();
String name = cursor.getString(nameIdx);
String phoneNumber = cursor.getString(phoneNumberIdx);
String image = cursor.getString(photoIdIdx);
System.out.println("Id--->"+contactid+"Name--->"+name);
System.out.println("Id--->"+contactid+"Name--->"+name);
System.out.println("Id--->"+contactid+"Number--->"+phoneNumber);
if (!phoneNumber.contains("*")) {
hashMap.put("contactid", "" + contactid);
hashMap.put("name", "" + name);
hashMap.put("phoneNumber", "" + phoneNumber);
hashMap.put("image", "" + image);
// hashMap.put("email", ""+email);
if (hashMapsArrayList != null) {
hashMapsArrayList.add(hashMap);}
// hashMapsArrayList.add(hashMap);
}
}
} while (cursor.moveToNext());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}