【发布时间】:2019-01-20 04:53:02
【问题描述】:
所以,我有一个应用程序可以创建包含标题和描述的文本注释。因为我有一个“Note”类,所以我使用了带有 SharedPreferences 的 gsonToJson(反之亦然)来保存和加载 Notes 的 ArrayList。笔记的创建和保存工作正常,但现在我想编辑用户选择的笔记。
这是创建 Note 的代码:
public class TextNote_creation extends AppCompatActivity {
public EditText title;
public EditText desc;
public List<Note> notes;
SharedPreferences sharedPreferences;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.textnote_create, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.confirmTextNote:
notes = new ArrayList<>();
String key = "notes";
String response;
Gson gson = new Gson();
sharedPreferences = TextNote_creation.this.getSharedPreferences("com.example.rui.trabalhopjdm", Context.MODE_PRIVATE);
notes.add(new Note(title.getText().toString(), title.getText().toString(), android.R.drawable.ic_menu_agenda));
SharedPreferences.Editor prefsEditor;
String jsonNotes = gson.toJson(notes);
prefsEditor = sharedPreferences.edit();
prefsEditor.putString(key , jsonNotes);
prefsEditor.apply();
Intent intent = new Intent(TextNote_creation.this, Notes.class);
startActivity(intent);
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_note_creation);
title = (EditText) findViewById(R.id.createTitle);
desc = (EditText) findViewById(R.id.createDesc);
}
}
在编辑活动中,我已经有了加载文件并将其放入注释数组的代码:
public class TextNote extends AppCompatActivity {
public EditText noteEditor;
public List<Note> notes = new ArrayList<>();
public EditText title;
public int noteId;
public int noteIndex;
SharedPreferences sharedPreferences;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.save_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.saveNote:
sharedPreferences = TextNote.this.getSharedPreferences("com.example.rui.trabalhopjdm", Context.MODE_PRIVATE);
Gson gson = new Gson();
for(int i = 0; i < notes.size(); i++){
if(i == noteIndex){
notes.get(i).setTitle(title.getText().toString());
notes.get(i).setDesc(noteEditor.getText().toString());
}
}
SharedPreferences.Editor editor = sharedPreferences.edit();
String jsonNotes = gson.toJson(notes);
Intent intent = new Intent(TextNote.this, Notes.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_note);
noteEditor = (EditText) findViewById(R.id.editText);
title = (EditText) findViewById(R.id.title);
Intent intent = getIntent();
noteId = intent.getIntExtra("noteId", -1);
noteIndex = noteId - 1;
if (noteId != -1){
title.setText(Notes.notes.get(noteId).getTitle());
noteEditor.setText(Notes.notes.get(noteId).getDesc());
}
sharedPreferences = TextNote.this.getSharedPreferences("com.example.rui.trabalhopjdm", Context.MODE_PRIVATE);
String key = "notes";
String response = "";
Gson gson = new Gson();
response = sharedPreferences.getString(key, null);
if (response == null)
{
}else{
notes = gson.fromJson(response, new TypeToken<List<Note>>(){}.getType());
}
}
}
所以,目前,我只获得与我通过 Intent 传递的 Id 相关联的 Note 并且更改的是值,但是现在,我如何使用这些值来编辑保存在 JSON 文件中的 Note?
【问题讨论】:
标签: java android json sharedpreferences