gpt4 book ai didi

java - 持久保存数据

转载 作者:行者123 更新时间:2023-12-02 12:15:35 25 4
gpt4 key购买 nike

我有一个笔记应用程序,我通过它实现了两种类型的 View : ListView 和 GridView 。用户可以根据自己的选择在 listViewgridView 之间切换。我遇到的问题是,我一直在尝试持久保存 View 的状态,以便在启动时打开所选 View 。我正在尝试使用 SharedPreferences 来实现此目的。我的代码出了什么问题?

private static final String KEY_NAME = "viewState";
private ListView mListNotes;
private GridView mGridNotes;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
private boolean mViewIsChanged = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the layouts for list/grid
mListNotes = (ListView) findViewById(R.id.main_listview);
mGridNotes = (GridView) findViewById(R.id.main_gridview);
// Retrieve value from Shared Preferences.
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
mViewIsChanged = sharedPreferences.getBoolean(KEY_NAME, false);
if (!mViewIsChanged){
mListNotes.setVisibility(View.VISIBLE);
mGridNotes.setVisibility(View.GONE);}
else {
mListNotes.setVisibility(View.GONE);
mGridNotes.setVisibility(View.VISIBLE);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customTitleView = inflater.inflate(R.layout.dialog_menu, null);
LinearLayout mListViewSelect = (LinearLayout) customTitleView.findViewById(R.id.list_select);
LinearLayout mGridViewSelect = (LinearLayout) customTitleView.findViewById(R.id.grid_select);
switch (item.getItemId()) {
case R.id.addItem:
// start NoteActivity
startActivity(new Intent(this, NoteActivity.class));
break;
case R.id.changeView:
final AlertDialog alertbox = new AlertDialog.Builder(this).create();
alertbox.setCancelable(true);
alertbox.setView(customTitleView);
alertbox.show();
mListViewSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


// Saving Data
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putBoolean(KEY_NAME, mViewIsChanged);
editor.apply();
mListNotes.setVisibility(View.VISIBLE);
mGridNotes.setVisibility(View.GONE);
alertbox.dismiss();
}
});
mGridViewSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// saving Data in SharedPreferences
mViewIsChanged = true;
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putBoolean(KEY_NAME, mViewIsChanged);
editor.apply();
mListNotes.setVisibility(View.GONE);
mGridNotes.setVisibility(View.VISIBLE);
alertbox.dismiss();
}
});

最佳答案

在你的onCreate()之后

mViewIsChanged = sharedPreferences.getBoolean(KEY_NAME, false);

mListNotes = (ListView) findViewById(R.id.main_listview);
mGridNotes = (GridView) findViewById(R.id.main_gridview);

添加这个

If (!mViewIsChanged){
mListNotes.setVisibility(View.VISIBLE);
mGridNotes.setVisibility(View.GONE);}
else {
mListNotes.setVisibility(View.GONE);
mGridNotes.setVisibility(View.VISIBLE);
}

或者将我在设置适配器后提供的逻辑放在你的 onResume() 中希望有帮助。

关于java - 持久保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46205751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com