Android的ListView控件记得先前所选项目

问题描述 投票:0回答:2

我有一个ListView填充的String array。用户选择一个项目,它的背景颜色的变化。现在,用户点击back button移动并回到mainActivity,再次回到listviewActivity。选定的项目不是在listView了显示。

android android-listview
2个回答
0
投票

您可以使用SharedPreferences保存选中的列表项的索引和检索它放回onCreate回调。或者你也可以使用Room persistence到索引存储到数据库和onCreate回调检索。


0
投票

你需要改变你的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>();

使用上面ListAdapter

当选择它更新boolean标志值设置为true。

数据Bean保存到任何存储在Android,像

SQLite

SharedPresence

local File

接下来,当你从本地数据库加载数据到同一个适配器取值,并与新的数据映射,并设置为ListView项目,像colourCode与参数boolean flag value (true)

© www.soinside.com 2019 - 2024. All rights reserved.