【问题标题】:SharedPreferences - edit object values saved on a Json fileSharedPreferences - 编辑保存在 Json 文件中的对象值
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 我知道它现在是如何工作的。谢谢。所以,如果我有一个登录页面,我可以使用帐户的用户名作为共享首选项的密钥,为每个用户制作一个 ArrayList 的注释吗?
    • 是的,你可以做那件事,唯一的事情你应该记住它应该是一个唯一的密钥,登录后如果你从服务器端获取任何用户ID,那么你也可以使用它
    • @RuiPascoal 如果您觉得答案有帮助,请点赞:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 2020-08-07
    • 2015-05-13
    • 2020-10-28
    • 1970-01-01
    • 2021-09-30
    相关资源
    最近更新 更多