使用简单适配器编辑ListView中的项目

时间:2015-11-04 13:46:42

标签: android listview simpleadapter

我使用SimpleAdapter填充了ListView:

List<Map<String, String>> listItems = new ArrayList<>();
listItems.add(createItemMap(R.string.rename_mobtag, R.string.rename_mobtag_help));
listItems.add(createItemMap(R.string.change_icon, R.string.change_icon_help));
listItems.add(createItemMap(R.string.become_invisible, R.string.become_invisible_help));

ListView mobtagSettings = (ListView) findViewById(R.id.mobtag_settings);
    final String[] fromMapKey = new String[]{TITLE_KEY, HELP_KEY};
    final int[] toLayoutId = new int[]{android.R.id.text1, android.R.id.text2};
    SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, android.R.layout.simple_list_item_2, fromMapKey, toLayoutId);
    mobtagSettings.setAdapter(simpleAdapter);

我需要更新特定项目的TextViews以响应外部事件(因此我无法使用Click侦听器)。如何获取TextViews以便我可以更改文本?

2 个答案:

答案 0 :(得分:1)

只需创建一个包含更改的新列表并更新适配器中的列表并调用notifyDatasetChanged方法

答案 1 :(得分:1)

对于特定的TextView项目,您知道列表中该项目的位置/索引吗?您可能想要创建自己的自定义适配器,该适配器将包含适配器列表中该特定项的更新功能,以便您可以更新该项的Textview。

您可以参考http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown_custom编写者有很多关于列表视图的内容,例如自定义适配器等。希望它有所帮助。

干杯