Skip to content Skip to sidebar Skip to footer

How To Add Subitems In A Listview

I'm trying to add subItems in my ListView. My listView should be organized with emails for items and their institution for the subitems, but with the following code I just added i

Solution 1:

You need a custom adapter with two textviews and in its getView() method set the appropriate data to each of your textviews.

Also by now you are passing to your adapter only the emails array, you'll need a different structure to include institutions too.

Solution 2:

The right way to do that is to create a HashMap for each item: Look the code below:

List<Login> listEmails = JsonUtil.getAllEmails(json);

                ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(listEmails.size());

                for (Login loginObj : listEmails) {

                    HashMap<String, String> item = new HashMap<String, String>();
                    item.put("email", loginObj.getEmailAndress());
                    item.put("institution", loginObj.getInstitution());

                    list.add(item);
                }

                String[] from = newString[] { "email", "institution" };

                int[] to = newint[] { android.R.id.text1, android.R.id.text2 };

                int nativeLayout = android.R.layout.two_line_list_item;

                emailListView.setAdapter(new SimpleAdapter(this, list, nativeLayout , from, to));

Post a Comment for "How To Add Subitems In A Listview"