我有一个ListView
填充的String array
。用户选择一个项目,它的背景颜色的变化。现在,用户点击back button
移动并回到mainActivity
,再次回到listviewActivity
。选定的项目不是在listView
了显示。
您可以使用SharedPreferences保存选中的列表项的索引和检索它放回onCreate
回调。或者你也可以使用Room persistence到索引存储到数据库和onCreate
回调检索。
你需要改变你的String[]
有两个新项目,以ArrayList
与字符串值沿着你有填充在Listview
首先需要做一个Bean类
class Data{
private String colourName;
private String colourCode;//int colourCode
private boolean ;
//make setter and getter methods
}
使
List<Data> colourDataList =new ArrayList<Data>();
使用上面List
与Adapter
,
当选择它更新boolean
标志值设置为true。
数据Bean保存到任何存储在Android
,像
SQLite
SharedPresence
local File
接下来,当你从本地数据库加载数据到同一个适配器取值,并与新的数据映射,并设置为ListView
项目,像colourCode
与参数boolean
flag
value (true)
。