将HashMap <string,string>放入ListVIew,其中包含节头</string,string>

时间:2014-12-15 18:04:29

标签: java android listview hashmap

我有这个数据的HashMap:

{Chefs Choice=Vegetable Samosa with Yogurt Sauce},
{Chefs Choice=Tomato Red Pepper Chutney}, 
{Cold Cereal=Miscellaneous/Peripherals}, {Dessert=Cheesecake Slice}, 
{Dessert=Banana Cream Pie with Caramel Topping}

现在我想把这些数据放到一个看起来像这样的ListView中。

enter image description here

因此,键,s都是节标题,然后每个键的值都在该节标题中。我想使用基础适配器但不太确定如何正确使用它。或者,如果有更好的方式来组织我的数据。我也对此持开放态度。

感谢您的帮助:)

以下是我添加数据的方法:

ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>();
while (eventType != XmlResourceParser.END_DOCUMENT) {
            String tagName = xmlData.getName();
            HashMap<String, String> blogPost = new HashMap<String, String>();

            switch (eventType) {
                case XmlResourceParser.START_TAG:
                    if (tagName.equalsIgnoreCase("day")) {
                        currentDay = xmlData.getAttributeValue(null, "name");
                    }
                    if (tagName.equalsIgnoreCase("meal")) {
                        currentMeal = xmlData.getAttributeValue(null, "name");
                    }
                    if (tagName.equalsIgnoreCase("counter") && currentDay.equalsIgnoreCase(day) && currentMeal.equalsIgnoreCase(meal)) {
                        mealArray.add(xmlData.getAttributeValue(null, "name"));
                        counter = xmlData.getAttributeValue(null, "name");
                    }

                    break;
                case XmlResourceParser.TEXT:
                    if (currentDay.equalsIgnoreCase(day) && currentMeal.equalsIgnoreCase(meal)) {
                        if (xmlData.getText().trim().length() > 0) {
                            Log.i(TAG, blogPost + "");
                            blogPost.put(counter, xmlData.getText());
                            blogPosts.add(blogPost);
                        }

                    }
                    break;

                case XmlPullParser.END_TAG:
                    recordsFound++;
                    break;
            }
            eventType = xmlData.next();
        }

1 个答案:

答案 0 :(得分:0)

您可以将主标题标记存储为arraylist。 数据结构将如下:

hashmap的主标签将来自arraylist,它的值将再次列出hashmap ...

喜欢标签&#39;厨师选择&#39;:蔬菜ssamosa和番茄红

&#13;
&#13;
// list contain your top header.....
List<String> arr=new ArrayList<String>();
//list to store complete data structure.	
List<HashMap<String, List<HashMap<String, String>>>> list =new ArrayList<HashMap<String,List<HashMap<String,String>>>>();


// setting the data structure
HashMap<String, List<HashMap<String, String>>> hash=new HashMap<String, List<HashMap<String,String>>>();
		List<HashMap<String, String>> hash1=new ArrayList<HashMap<String,String>>();
		HashMap<String, String> hash2=new HashMap<String, String>();
		hash2.put("tag", "vegetable");
		hash1.add(hash2);
    // getting tag from tag list......
		hash.put(list.get(i).toString(), hash1);
		list.add(hash);
&#13;
&#13;
&#13;