0

UPDATE3

Here I am getting all contacts of user phone and whatsapp number. after successfully retrieving I am getting IndexOutOfBoundsException.

Code

Map<String, String> namePhoneMap = new HashMap<String, String>();

    Cursor phones = getContentResolver()
            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null, null, null, null);

    // Loop Through All The Numbers
    while (phones.moveToNext()) {

        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        // Cleanup the phone number
        phoneNumber = phoneNumber.replaceAll("[()\\s-]+", "");

        // Enter Into Hash Map
        namePhoneMap.put(phoneNumber, name);

    }

    for (Map.Entry<String, String> entry : namePhoneMap.entrySet()) {
        String key = entry.getKey();
        Log.d("Login", "Phone :" + key);
        String value = entry.getValue();
        Log.d("Login", "Name :" + value);
        phoneContacts.add(key);
        nameContacts.add(value);
    }


    phones.close();


//ArrayList for Store Whatsapp Contact
Map<String, String> whatsappMap = new HashMap<String, String>();

final String[] projection = {
        ContactsContract.Data.CONTACT_ID,
        ContactsContract.Data.DISPLAY_NAME,
        ContactsContract.Data.MIMETYPE,
        "account_type",
        ContactsContract.Data.DATA3,
};

final String selection = ContactsContract.Data.MIMETYPE + " =? and account_type=?";
final String[] selectionArgs = {
        "vnd.android.cursor.item/vnd.com.whatsapp.profile",
        "com.whatsapp"
};

ContentResolver cr = getContentResolver();
Cursor c = cr.query(
        ContactsContract.Data.CONTENT_URI,
        projection,
        selection,
        selectionArgs,
        null);

while (c.moveToNext()) {
    String id = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID));
    String number = c.getString(c.getColumnIndex(ContactsContract.Data.DATA3));
    String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

    Log.d(TAG, "name " +name + " - number - "+number);

    whatsappMap.put(name, number);

}
Log.d(TAG, "Total WhatsApp Contacts: " + c.getCount());

for (Map.Entry<String, String> entryWhatsapp : whatsappMap.entrySet()) {
    String key = entryWhatsapp.getKey();
    Log.d(TAG, "PhoneWhatsApp :" + key);
    String value = entryWhatsapp.getValue();
    Log.d(TAG, "NameWhatsApp :" + value);

    myWhatsappContacts.add(value);
}



c.close();

Till Now my code is working very well. Now I am adding a code of adding three array in excel file.

String csv = (Environment.getExternalStorageDirectory().getAbsolutePath() + "/SendingContactList2.csv"); // Here csv file name is MyCsvFile.csv

        Log.d("Login3", csv);
        CSVWriter writer = null;
        try {
            writer = new CSVWriter(new FileWriter(csv));

            List<String[]> data = new ArrayList<String[]>();
//            data.add(new String[]{"number","name"});

            for (int i = 0; i < phoneContacts.size(); i++){
//
                if (Integer.parseInt(String.valueOf(phoneContacts.get(i).length())) < 10) {
                    Log.d("Login34", String.valueOf(phoneContacts.get(i).length()));
                    phoneContacts.remove(i);
                    nameContacts.remove(i);
                } else {               
                    data.add(new String[]{phoneContacts.get(i),nameContacts.get(i)});// Line no 1


//                data.add(new String[]{phoneContacts.get(i),nameContacts.get(i),myWhatsappContacts.get(i)}); // Line no 2
}
            }

            writer.writeAll(data); // data is adding to csv

            writer.close();
//            callRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

Now when I run Line 1 code and when I run Line 2 It gives me Index out of bound exception.

BlackBlind
  • 772
  • 9
  • 26

1 Answers1

2

1:

    import java.util.Arrays;

    for(String s : array2) {
        if(Arrays.asList(array1).contains(s))
           System.out.println(s);
    }

2:

    for(String s2 : array2) {
       for(String s1: array1) {
          if(s1.equals(s2)) {
            System.out.println(s1 + ", " + s2);
          }
       }
    }

UPDATE2

if (phoneContacts.get(i).length < 10) {
    Log.d("Login34", String.valueOf(phoneContacts.get(i).length()));
    phoneContacts.remove(i);
    nameContacts.remove(i);
} else {
    String phone = phoneContacts.size() - 1 < i ? phoneContacts.get(i) : "0";
    String name = nameContacts.size() - 1 < i ? nameContacts.get(i) : "0";
    String third = thirdarray.size() - 1 < i ? thirdarray.get(i) : "0";
    data.add(new String[]{ phone, name, third }); 
}
P.Juni
  • 2,365
  • 14
  • 26
  • 1
    Instead of looping through first array, loop through second array which is small in size would be better. Update your answer – Rohit Suthar Apr 30 '19 at 08:00
  • can you explain else condition – BlackBlind May 01 '19 at 06:57
  • this code showing this error Caused by: java.lang.IndexOutOfBoundsException: Index: 200, Size: 200 on else condition third line – BlackBlind May 01 '19 at 07:15
  • Else condition -> we check if the size - 1 of the array is smaller than the i variable, if so, then take value from array at position i, if not then take "0". But it’s just a blind guess. Post whole code – P.Juni May 01 '19 at 07:21