在ArrayList中添加ArrayList并检索它

时间:2017-03-13 13:11:57

标签: java android arraylist

我正在使用此代码将ArrayList添加到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);

我检查调试器模式它成功地将联系人添加到contactsList,但我使用此代码来检索它:

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

它返回一个空的arraylist。

完整代码

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

}

3 个答案:

答案 0 :(得分:3)

你的变量只有一个错字。您在contactsList上添加联系人数据,然后移交索引元素&#39; 0&#39;来自contactList(没有s)。

编辑: 如果您想使用联系人列表,您应该创建一个自己的类Contact,您可以在其中构建分离的对象。 List in List逻辑和约定可能非常容易混淆并且容易出错。

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

答案 1 :(得分:1)

问题是,你清除了你引用的集合&#34; contactList.clear(); &#34; 尝试删除此行。

如果您需要保留集合,则需要创建一个新的实例:

batched_tensordot

答案 2 :(得分:0)

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