我是这个多选列表视图的新手。我想在列表视图中保存复选框的选中状态,以便在用户关闭应用程序然后再次打开时,所选复选框仍保持选中状态。有没有办法做到这一点。我搜索了它,发现它可以使用SharedPreference完成,但我没有获得有关如何使用它的更多信息。感谢
Query
答案 0 :(得分:1)
例如,您可以在SharedPreferences中保存状态。
因此,您的onCreate
和onDestroy
方法将如下所示:
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPrefs", MODE_PRIVATE);
@Override
protected void onCreate(final Bundle savedInstanceState) {
...
Set<String> checkedItemsSource = sharedPreferences.getStringSet("checked_items", new HashSet<String>());
SparseBooleanArray checkedItems = convertToCheckedItems(checkedItemsSource);
for (int i = 0; i < checkedItems.size(); i++) {
int checkedPosition = checkedItems.keyAt(i);
listView.setItemChecked(checkedPosition, true);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
Set<String> stringSet = convertToStringSet(checkedItems);
sharedPreferences.edit()
.putStringSet("checked_items", stringSet)
.apply();
}
private SparseBooleanArray convertToCheckedItems(Set<String> checkedItems) {
SparseBooleanArray array = new SparseBooleanArray();
for(String itemPositionStr : checkedItems) {
int position = Integer.parseInt(itemPositionStr);
array.put(position, true);
}
return array;
}
private Set<String> convertToStringSet(SparseBooleanArray checkedItems) {
Set<String> result = new HashSet<>();
for (int i = 0; i < checkedItems.size(); i++) {
result.add(String.valueOf(checkedItems.keyAt(i)));
}
return result;
}
答案 1 :(得分:0)
在列表项上设置复选框,如下所示
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
AppListOfAllApps hideSelectedApps = (AppListOfAllApps) parent.getItemAtPosition(position);
if (view != null)
{
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
hideSelectedApps = (AppListOfAllApps) checkBox.getTag();
hideSelectedApps.setSelected(!checkBox.isChecked());
checkBox.setChecked(!checkBox.isChecked());
}
}
});
将所选项目存储在共享优先级中,然后按下按钮匹配列表中的所有当前项目与存储在共享优先级中的项目之间的比较
boolean chkbox = false;
String selecteditem = SharedPreferences.getSharedPref(getApplicationContext()).selecteditem();
然后就这样做
if (allitems.contains(selecteditem ))
{
chkbox = true;
}
else
{
chkbox = false;
}
现在将其传递给适配器构造函数 这样可以保存复选框状态并重新启动它
答案 2 :(得分:0)
为此,您需要保持模型中的状态。并且在每个onBindView调用上,只需按模型重置状态。 有关详细信息,您可以从 -
获取帮助https://stackoverflow.com/a/38182528/1741586
希望它会对你有所帮助:)。
答案 3 :(得分:0)
首先,您需要维护所选值的布尔数组,然后您可以将其存储在共享首选项中,并且还需要自定义适配器,同时检查Multiple Checkbox values in listview storing & retrieving using sharedpreferences
活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] listArray = new String[] { //Add String values };
SharedPreferences sharedPreferences = getSharedPreferences("status", MODE_PRIVATE);
Boolean[] checkedStatus = new Boolean[listArray.length];
for ( int index = 0; index < checkedStatus.length; index++)
checkedStatus[index] = sharedPreferences.getBoolean(Integer.toString(index), false);
ListView listView = (ListView) findViewById(R.id.listview);
CustomAdapter adapter = new CustomAdapter(this, R.layout.row_layout, listArray, checkedStatus);
listView.setAdapter(adapter);
}
试试这种方式
public class CustomAdapter extends ArrayAdapter<String> implements CompoundButton.OnCheckedChangeListener{
String[] values;
Boolean[] checkedStatus;
public CustomAdapter(Context context, int resource, String[] values, Boolean[] checkedStatus) {
super(context, resource, values);
this.values = values;
this.checkedStatus = checkedStatus;
}
@Override
public int getCount() {
return values.length;
}
@Override
public String getItem(int position) {
return values[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_layout, null);
}
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText(values[position]);
CheckBox box = (CheckBox) view.findViewById(R.id.chk);
box.setTag(position);
box.setOnCheckedChangeListener(this);
box.setChecked(checkedStatus[position]);
return view;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Integer index = (Integer) buttonView.getTag();
checkedStatus[index] = isChecked;
String key = index.toString();
SharedPreferences sharedPreferences=getContext().getSharedPreferences("status", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putBoolean(key,isChecked);
editor.apply();
}