0
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

4
  • You declare array as empty array String[] name = new String []{}; Instead use - String[] name = new String [SIZE]{}; Commented Apr 15, 2021 at 6:05
  • @VivekSwansi When I print out Array to String I got output something just like this [Ljava.lang.String;@2177f8dc If I send array to php server.. Will php server get the value or values inside the Array? [10]; I have set Array size.. How can I make it default..? Like user may have more than 1000 contacts. So, what I have to set instead of the size? Commented Apr 15, 2021 at 6:12
  • your code has a lot of issues.... increasing the initial size of the String[] is not a good solution as you might have devices with more than 10000 contacts, and then your app will crash. Commented Apr 18, 2021 at 7:29
  • do you even need and use the phone number you're getting from the cursor? Your code can be much simpler and more efficient. Commented Apr 18, 2021 at 7:30

2 Answers 2

0
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.!

Sign up to request clarification or add additional context in comments.

Comments

0

I would not recommend guessing the array size you'll need for two reasons:

  1. Your app may crash if it runs on a device with more then 10,000 contacts
  2. You're allocating a huge string array, even if the number of contacts is very small

The best approach would be to start with an ArrayList (which has flexible length), and then convert it to the String array you need.

Also, it's advised to add a projection to all your queries to make them faster and use less memory.

// Start with an ArrayList as we don't know the size we'll need
List<String> names = new ArrayList<>();

String[] projection = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER};
Cursor c = getContentResolver().query(Phone.CONTENT_URI,projection,null,null,DISPLAY_NAME + " ASC ");

while (c.moveToNext()){
    String displayName = c.getString(0);
    String number = c.getString(1);

    names.add(displayName);
    Log.d("CONTACTS_NAME", "Found: " + displayName + ", with number " + number);
}

c.close();

// convert the ArrayList to a String array
String[] namesArr = names.toArray(new String[]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.