0

我有一个班级食谱清单:

List<Recipe> recipeList = new ArrayList<Recipe>();

和类 whit 属性基础:

 public class Recipe {
        public int id;
        public String name;
        public String time;
...

我尝试将它放在 SimpleAdapter 中,但是当我这样做时,Android Studio 会出现错误:

 ListAdapter listAdapter = new SimpleAdapter(
ResultActivity.this,
recipeList,R.layout.list_item,
new String[] {"id","name","time"},
new int[] { R.id.id,R.id.name,R.id.time  }
                    );

我在 Map 中看到了一些对其进行转换的示例,但是如何直接使用类 how 参数进行制作,非常感谢这节省了我的一天。

4

1 回答 1

1

以这种方式创建数据映射:

    ArrayList<Map<String, String>> mapData = 
        new ArrayList<Map<String, String>>();

    for (Recipe recipe : recipeList) {

        HashMap<String, String> map = new HashMap<String, String>();

        map.put("id", String.valueOf(recipe.id));
        map.put("name", recipe.name);
        map.put("time", recipe.time);

        mapData.add(map);

    }

这应该有效:

    ListAdapter listAdapter = new SimpleAdapter(ResultActivity.this, mapData,
        R.layout.list_item, new String[] { "id", "name", "time" },
        new int[] { R.id.id, R.id.name, R.id.time });
于 2013-10-19T21:48:41.163 回答