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

时间:2019-01-20 04:53:02

标签: java android json sharedpreferences

因此,我有一个应用程序,可以创建包含标题和描述的文本注释。由于我有一个“ Note”类,因此我将gsonToJson(反之亦然)与SharedPreferences一起使用来保存和加载Notes的ArrayList。笔记的创建和保存工作正常,但是现在我要编辑用户选择的笔记。

以下是创建注释的代码:

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,并且change是值,但是现在,如何使用这些值来编辑保存在JSON文件中的Note?

1 个答案:

答案 0 :(得分:0)

在共享首选项中,这些值存储在“键值”对中,因此,如果您仅将编辑后的值与相应的键一起保存,则先前的值将被覆盖,因为您已经在获取注释数组,因此可以保存一件事在新的ArrayList中添加数组列表,并在该索引上更新要获取注释的索引值,使用Gson转换新的arrayList并将其保存到“共享首选项”中。