Cursor c;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
c=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,ContactsContract.Contacts.DISPLAY_NAME+" ASC ");
int i=0;
while (c.moveToNext()){
//get contact list
String diplayName=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//put value into Hashmap
HashMap<String, String> user_data = new HashMap<>();
user_data.put(Constant.NAME, diplayName);
user_data.put(Constant.PHONE_NUMBER, number);
list.add(user_data);
// String[] arr = { "1", "2", "3" };
// System.out.println(Arrays.toString(arr)); // prints "[1, 2, 3]"
// arr = append(arr, "4");
// System.out.println(Arrays.toString(arr)); // prints "[1, 2, 3, 4]"
String[] name = new String []{c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))};
Log.d("CONTACTS_NAME", name[0]);
}
Log.d("CONTACTS_NAME_ARRAY_LIST", String.valueOf(list));
c.close();
I get output from CONTACTS_NAME
04-15 11:39:52.333 7623-7623/com.game.stakes D/CONTACTS_NAME: B
04-15 11:39:52.333 7623-7623/com.game.stakes D/CONTACTS_NAME: Bs
But, the output isn't very helpful.. So, I tried
String[] name = new String []{};
while (c.moveToNext()){
.....
. .. . ..
.. .
name[i] = displayName;
i++;
}
This time I just get error
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
I thought I could count contact list c.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID)
. But, I tried lot of way to count none of them are working.
My question is how can I add displayName to a single array?
I don't want any answer on ArrayList
. Cause, I have already worked with it and, I want to add them to Array
not ArrayList
String[] name = new String [10000]{};
while (c.moveToNext()){
.....
. .. . ..
.. .
name[i] = displayName;
i++;
}
Log.d("COUNTS_CONTACTS", Arrays.toString(name));
Output :
[B, Bs, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, .....]
I wasn't actually declaring array size. So, when I added array size. Then, print out again it was working fine.!