gpt4 book ai didi

java - Gson 的 SharedPreferences 无法正常工作

转载 作者:行者123 更新时间:2023-12-01 17:26:07 24 4
gpt4 key购买 nike

我正在尝试保存我的项目 ArrayList ArrayList<MyItem> items通过Gson但是当我重新打开应用程序时,一切都消失了,并且没有加载任何数据。

这是我的代码(我正在使用片段)

public class FragmentPage extends Fragment {

private ExampleAdapter mAdapter;
private EditText editTask;
ArrayList<MyItem> items;
private Button insertbtn;
private Button stateButton;
private TextView taskText;
private String task;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
Bundle savedInstanceState) {
loadData();
CreateExampleList();


View view = inflater.inflate(R.layout.page_fragment, container, false);


RecyclerView recyclerView = view.findViewById(R.id.listRecyclerView);
taskText = view.findViewById(R.id.tv_country);
insertbtn = view.findViewById(R.id.button_insert);
editTask = view.findViewById(R.id.editTask);
stateButton = view.findViewById(R.id.tv_button);

insertbtn.setEnabled(false);
editTask.addTextChangedListener(new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

String taskInput = editTask.getText().toString().trim();
insertbtn.setEnabled(!taskInput.isEmpty());

}

@Override
public void afterTextChanged(Editable s) {
task = editTask.getText().toString().trim();

}
});


insertbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InsertItem();
SaveData();
editTask.setText(null);


}
});


recyclerView.setHasFixedSize(true);

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemViewCacheSize(500);
mAdapter = new ExampleAdapter(items);
recyclerView.setAdapter(mAdapter);


return view;
}

private void SaveData() {
SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("shared
preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(items);
editor.putString("task list", json);
editor.apply();
}

private void loadData() {
SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("shared
preferences", MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("task list", null);
Type type = new TypeToken<ArrayList<MyItem>>() {
}.getType();
items = gson.fromJson(json, type);

if (items == null) {
items = new ArrayList();
}

}

public void InsertItem() {

items.add(new MyItem(task, stateButton));
mAdapter.notifyItemInserted(items.size());


}

private void CreateExampleList() {
items = new ArrayList<MyItem>();
}

ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder
viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
items.remove(viewHolder.getAdapterPosition());
mAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());

}
};
}

最佳答案

我自己找到了答案。

这太简单了,连我都不敢相信......

我只需将 loadData() 移动到 View view = inflater.inflate(R.layout.page_fragment,container, false);

关于java - Gson 的 SharedPreferences 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61208512/

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