0

I am using this code to add ArrayList into ArrayList>

ArrayList<String> contact = new ArrayList<String>();
ArrayList<ArrayList<String>> contactsList = new ArrayList<ArrayList<String>>();
contact.add("name1");
contact.add("name2");
contact.add("name3");
contactsList.add(contact);

I check in debugger mode it successfully adds contact into contactsList, but I use this code to retrieve it:

ArrayList<String> list = contactsList.get(0);

It returns an empty arraylist.

Full code

public class ContactsFragment extends Fragment {

ArrayList<ArrayList<String>> contactsList = new ArrayList<ArrayList<String>>();
ArrayList<String> contactList = new ArrayList<String>();

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ContentResolver resolver = getActivity().getContentResolver();
    Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    while (cursor.moveToNext()) {
        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        contactList.add(name);
        Cursor phoneCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
        while (phoneCursor.moveToNext()) {
            String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contactList.add(phoneNumber);
        }

        contactsList.add(contactList);
        contactList.clear();


    }

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    String data = "";

    for (int i = 0; i < contactsList.size(); i++) {
        ArrayList list = new ArrayList();
        list.add(contactsList.get(i));
        for (int q = 0; q < list.size(); q++) {
            data = data + list.get(q) + "\n";
        }
        data = data + "\n\n";
    }

    builder.setMessage(data);
    builder.create();
    builder.show();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);

    return rootView;
}

}

2
  • Copying and pasting the same code works for me. contactList has one element and that one element list has 3 elements. Commented Mar 13, 2017 at 13:14
  • Hint: instead of writing ArrayList<ArrayList<String>> contactsList = new ArrayList<ArrayList<String>>(); go for List<List<String>> contactsList = new ArrayList<>(); ... you dont need to repeat the type information the right hand side; and on the other hand, you dont want to use the specific implementation class name as type on the left hand side. Commented Mar 13, 2017 at 14:12

3 Answers 3

3

There is just a typo at your variable. You add the contact data on contactsList and then hand over the index element '0' from contactList (without s).

Edit: If you want to work with a list of contacts you should maybe create a own class Contact where you can build separated objects. The List in List logic and conventions can be very confusing and vulnerable to errors.

Contact c1 = new Contact("Name1");
Contact c2 = new Contact("Name2");
Contact c3 = new Contact("Name3");

ArrayList<Contact> contactList = new ArrayList<Contact>();
contactList.add(c1);
contactList.add(c2);
contactList.add(c3);

for (int i = 0; i < contactList.size(); i++) {
     System.out.println(contactList.get(i).name1);
     // your code...
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried it by myself and it works fine. Can you send your real code to go deeper here? Maybe with exception message as well.
have a look at it
Ok. It's pretty hard to find the wrongdoer without debug function here. Check my edited post, maybe it could help you further.
1

The problem is, you clear the collection you reference to "contactList.clear(); " Try to remove this line.

If you need to keep the collection, you need to create a new instance of it:

List<ArrayList<String>> l1 = new ArrayList<>();
    ArrayList<String> l2 = new ArrayList<>();
    l2.add("name1");
    l2.add("name2");
    l1.add(new ArrayList<>(l2));

Comments

0
ArrayList<String> contact = new ArrayList<String>();
contact.add("name1");
contact.add("name2");
contact.add("name3");
String contacts = contact.get(0).toString();

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.