简体   繁体   English

SharedPreferences - 编辑保存在 Json 文件中的对象值

[英]SharedPreferences - edit object values saved on a Json file

So, I have an App that creates text Notes that contain a title and a description.所以,我有一个应用程序可以创建包含标题和描述的文本注释。 Since I have a Class "Note" I used gsonToJson(and vice-versa) with SharedPreferences to save and load the ArrayList of Notes.由于我有一个“Note”类,我使用 gsonToJson(反之亦然)和 SharedPreferences 来保存和加载 Notes 的 ArrayList。 The creation and saving of the notes work fine but now I want to edit the note the users choose.笔记的创建和保存工作正常,但现在我想编辑用户选择的笔记。

Here is the code to create the 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);


}
}

In the Edit Activity, I have already the code to load the file and put it into an Array of Notes:在编辑活动中,我已经有了加载文件并将其放入注释数组的代码:

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());
    }


}
}

So, for now, I only get the Note associated with the Id I passed by the Intent and change is values, but now, how do I use those values to edit the Note saved in the JSON file?所以,现在,我只得到与我通过 Intent 传递的 Id 关联的 Note 并更改值,但是现在,我如何使用这些值来编辑保存在 JSON 文件中的 Note?

在共享首选项中,值存储在键值对中,因此如果您只使用相应的键保存编辑过的值,那么先前的值将被覆盖,因为您已经获得了笔记数组,您可以做一件事保存数组列表在一个新的 ArrayList 中并更新您从中获取注释的索引的编辑值,使用 Gson 转换新的 arrayList 并将其保存到共享首选项中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM