我正在尝试为每个列表项添加一个不同的图标,但是遇到了麻烦。想法是将每个列表视图项放在一起以使编辑更加容易,但是事实证明,添加图像比我想象的要复杂。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ArrayList<Map<String, String>> list = buildData();
    String[] from = { "title", "description" };
    int[] to = { android.R.id.text1, android.R.id.text2 };

    SimpleAdapter adapter = new SimpleAdapter(this, list,
            android.R.layout.simple_list_item_2, from, to);
    setListAdapter(adapter);
}

private ArrayList<Map<String, String>> buildData() {
    ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
    list.add(putData("Title 1", "Description 1"));
    list.add(putData("Title 2", "Description 2"));
    list.add(putData("Title 3", "Description 3"));
    return list;
}

private HashMap<String, String> putData(String name, String purpose) {
    HashMap<String, String> item = new HashMap<String, String>();
    item.put("name", name);
    item.put("purpose", purpose);
    return item;
}

最佳答案

如果不是不可能的话,那么在您当前的设置中至少会很麻烦。

顾名思义,SimpleAdapter是一个基本类,为您提供基本功能。也就是说,通常-具有文本视图的列表。

创建适配器时,您需要为其中的每个项目指定确切的布局(即android.R.layout_simple_list_item_2)。您不能将任何其他物品推到那里(除非您真的很固执)。

您需要什么:

  • 定制适配器(最好是)
  • 适配器
  • 的定制布局
  • 适配器中的数据源,它将特定的图标映射到每个元素。

  • 这是一个不错的演示:http://hmkcode.com/android-custom-listview-titles-icons-counter/

    10-07 19:40
    查看更多