如何在Listview中添加子项

时间:2016-06-04 16:53:44

标签: android listview

我有工作ListView我正在尝试在其中添加子项。但list.setAdapter(new SimpleAdapter(this, list, label, wala));

似乎存在问题

                String[] resName = new String[(int) dataSnapshot.getChildrenCount()];
                String[] resLoc;
                ListView list = (ListView) findViewById(R.id.listViewRest);
                restauList = new ArrayList<HashMap<String, String>>();
                resLoc = new String[]{"TRY", "TRY2"};

                int i = 0;
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                    resName[i] = dataSnapshot1.child("resname").getValue().toString();
                    HashMap<String, String> listahan = new HashMap<String, String>();
                    listahan.put("Restaurant Name", String.valueOf(resName));
                    listahan.put("Location", String.valueOf(resLoc));
                    restauList.add(listahan);
                    i++;
                }
                String [] label = new String[] {"Name", "Location"};
                int [] wala = new int []{ android.R.id.text1, android.R.id.text2 };

                int layout = android.R.layout.two_line_list_item;
                list.setAdapter(new SimpleAdapter(this, list, label, wala));

2 个答案:

答案 0 :(得分:0)

如果我没有错,你可以尝试使用expandable listview来浏览这个链接

https://stackoverflow.com/a/37636685/6312749

答案 1 :(得分:0)

您应该使用ExpandableListView在listView中添加subItem。