ListAdapter - SimpleAdapter whit a Class如何制作

时间:2013-10-19 18:57:07

标签: android android-listview android-arrayadapter

我有一份课程食谱清单:

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中改变这个例子的例子,但是如何直接在类中使用参数,这要归功于我今天的节省。

1 个答案:

答案 0 :(得分: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 });