Reputation: 2816
i am getting all contact names and number bur i just want to show a number and a name which i choose.
here is the code which return all contacts :
ArrayList<String> contactList = new ArrayList<String>();
switch (reqCode) {
case (0):
if (resultCode == Activity.RESULT_OK) {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor people = getContentResolver().query(uri, projection,
null, null, null);
int indexName = people
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
people.moveToFirst();
do {
String name = people.getString(indexName);
String number = people.getString(indexNumber);
String contact = name + "" + number;
contactList.add(contact);
} while (people.moveToNext());
}
in contactList
i want to add a number and a name
Upvotes: 0
Views: 1300
Reputation: 17580
As I understand your question you want add number and name in collection-Use HashMap for that.
HashMap<String,String> contactList = new HashMap<String,String>();
switch (reqCode) {
case (0):
if (resultCode == Activity.RESULT_OK) {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor people = getContentResolver().query(uri, projection,
null, null, null);
int indexName = people
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
people.moveToFirst();
do {
String name = people.getString(indexName);
String number = people.getString(indexNumber);
String contact = name + "" + number;
contactList.put(number,name);
} while (people.moveToNext());
}
Upvotes: 1