简体   繁体   中英

Add ArrayList in ArrayList and retrieve it

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;
}

}

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...
}

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));
ArrayList<String> contact = new ArrayList<String>();
contact.add("name1");
contact.add("name2");
contact.add("name3");
String contacts = contact.get(0).toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM