gpt4 book ai didi

java - 如何在android中使用MVVM模式在在线服务器上保存数据

转载 作者:行者123 更新时间:2023-12-02 03:02:55 26 4
gpt4 key购买 nike

我正在使用 MVVM 设计模式制作一个应用程序。最初我尝试在离线数据库中添加数据。现在我想将数据保存到在线服务器。

下面是我的离线数据存储代码:

NoteDao.java

@Dao
public interface NoteDao {

@Insert
void insert(Note note);

@Query("SELECT * FROM My_notes ORDER BY id DESC")
LiveData<List<Note>> getAllNotes();

}

NotesviewModel.java

public class NotesviewModel extends AndroidViewModel {

private NoteRepository noteRepository;
private LiveData<List<Note>> allNotes;

public NotesviewModel(@NonNull Application application) {
super(application);

noteRepository = new NoteRepository(application);
allNotes = noteRepository.getAllNotes();
}

public void insert(Note note){

noteRepository.insert(note);
}

public LiveData<List<Note>> getAllNotes(){

return allNotes;
}
}

NoteRepository.java

public class NoteRepository {

private NoteDao noteDao;
private LiveData<List<Note>> allNotes;

public NoteRepository(Application application){

DatabaseSingleton db = DatabaseSingleton.getInstance(application);
noteDao = db.noteDao();
allNotes = noteDao.getAllNotes();
}

public void insert(Note note){

new InsertNoteAsyncTask(noteDao).execute(note);
}

public LiveData<List<Note>> getAllNotes(){

return allNotes;
}

private static class InsertNoteAsyncTask extends AsyncTask<Note,Void,Void>{

private NoteDao noteDao;

private InsertNoteAsyncTask(NoteDao noteDao) {

this.noteDao = noteDao;
}

@Override
protected Void doInBackground(Note... notes) {

noteDao.insert(notes[0]);
return null;
}
}
}

AddNotes.java

 submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

saveNote();
}
});

private void saveNote(){

String str1 = title.getText().toString();
String str2 = description.getText().toString();

if(str1.equals("") || str2.equals("")){

Toast.makeText(getApplicationContext(),"Field cannot be empty",Toast.LENGTH_SHORT).show();
}else{

Intent i = new Intent(AddNotes.this,MainActivity.class);
i.putExtra("title",str1);
i.putExtra("desc",str2);
setResult(RESULT_OK,i);
finish();

}
}

MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == ADD_NOTE && resultCode == RESULT_OK){

if(data != null){

String title = data.getStringExtra("title");
String desc = data.getStringExtra("desc");

Note mynote = new Note(title,desc);
noteViewModel.insert(mynote);

Toast.makeText(getApplicationContext(),"Note saved",Toast.LENGTH_SHORT).show();
}else{

Toast.makeText(getApplicationContext(),"Unable to save note",Toast.LENGTH_SHORT).show();
}
}
}

MainActivity中,我使用startActivityForResult方法从AddNotes Activity获取数据。我想知道如何修改我的代码,以便可以将数据添加到在线服务器。请有人指导任何帮助,我们将不胜感激。

谢谢

最佳答案

创建2个存储库

  1. NoteRepositoryLocal
  2. NoteRepositoryLive

作为您的代码 NoteRepositoryLocal 类似于 NoteRepository添加新的存储库以供 API 调用。 (在您的项目中实现okhttp并改造API调用)

在你的ViewModel中,有两个方法调用。

noteRepositoryLocal.insert(note);
noteRepositoryLive.insert(note);

我没有添加 NoteRepositoryLive 相关代码。如果您有问题,请告诉我。

希望这个答案对你有帮助:)

关于java - 如何在android中使用MVVM模式在在线服务器上保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57040209/

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