我正在从服务器中获取数据并保存在会议室数据库中,然后从会议室中将其显示在回收站视图中。数据已完美地保存在会议室数据库中并在回收站视图中显示。
问题::当我从服务器数据库中删除某些数据时,其先前保存的旧副本仍然保留在房间中。
我想要的内容:我不想在回收器视图中显示从服务器删除的数据。因此,如何根据服务器的响应来更新会议室数据库。
这是我到目前为止所做的:
UserDao.java
@Dao
public interface UserDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void Insert(User... users);
@Query("SELECT * FROM Users")
LiveData<List<User>> getRoomUsers();
}
User.java
@Entity(tableName = "Users")
public class User {
@NonNull
@PrimaryKey
private String id;
@ColumnInfo(name = "name")
@SerializedName("name")
@Expose
private String name;
@ColumnInfo(name = "age")
@SerializedName("age")
@Expose
private String age;
public User(String id,String name, String age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
UserRepository.java
public class UserRepository {
private Context context;
private UserDb userDb;
private LiveData<List<User>> listLiveData;
public UserRepository(Context context) {
this.context = context;
userDb = UserDb.getInstance(context);
listLiveData = userDb.userDao().getRoomUsers();
}
public void getUserList(){
Retrofit retrofit = RetrofitClient.getInstance();
ApiService apiService = retrofit.create(ApiService.class);
Call<List<User>> userList = apiService.getUser();
userList.enqueue(new Callback<List<User>>() {
@Override
public void onResponse(Call<List<User>> call, final Response<List<User>> response) {
Completable.fromAction(new Action() {
@Override
public void run() throws Exception {
if(response.body() != null) {
List<User> list = response.body();
for (int i = 0; i < list.size(); i++) {
String id = list.get(i).getId();
String names = list.get(i).getName();
String age = list.get(i).getAge();
User user = new User(id,names,age);
userDb.userDao().Insert(user);
}
}
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
Toast.makeText(context,"Data inserted",Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable e) {
Toast.makeText(context,e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onFailure(Call<List<User>> call, Throwable t) {
Toast.makeText(context,t.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
public LiveData<List<User>> getRoomUsers(){
return listLiveData;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
UserRepository userRepository;
RecyclerView recyclerView;
UserViewModel userModel;
List<User> userList;
UserAdapter adapter;
ProgressBar prg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
prg = findViewById(R.id.prg);
userRepository = new UserRepository(this);
userModel = ViewModelProviders.of(this).get(UserViewModel.class);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
userList = new ArrayList<>();
adapter = new UserAdapter(userList,this);
recyclerView.setAdapter(adapter);
userModel.getListLiveData().observe(this, new Observer<List<User>>() {
@Override
public void onChanged(List<User> users) {
prg.setVisibility(View.INVISIBLE);
adapter.setUserList(users);
}
});
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,AddUser.class);
startActivity(i);
}
});
userRepository.getUserList();
}
请让我知道如何获得期望的结果。任何帮助将不胜感激。
谢谢
答案 0 :(得分:0)
使用getRoomUsers()
从onResponse()
中的存储库中获取已知用户的列表。
对于response.body()
中的每个用户,检查其在已知用户列表中是否具有相应的列表项。删除该项目。
然后,已知用户列表将仅包含已从服务器删除的用户。因此,对于其余所有这些用户,您都可以将其从会议室数据库中删除。
以下代码片段显示了如何查找服务器当前未知的用户:
if (response.body() != null) {
List<User> list = response.body();
List<User> previouslyKnownUsers = new ArrayList<>(userDb.userDao().getRoomUsers().getValue());
for (int i = 0; i < list.size(); i++) {
String id = list.get(i).getId();
String names = list.get(i).getName();
String age = list.get(i).getAge();
User user = new User(id, names, age);
userDb.userDao().Insert(user);
User knownUser = null;
for(User previouslyKnownUser: previouslyKnownUsers){
if(previouslyKnownUser.getId().equals(user.getId())){
knownUser = previouslyKnownUser;
break;
}
}
if(knownUser != null){
previouslyKnownUsers.remove(knownUser);
}
}
for(User currentlyUnknownUser: previouslyKnownUsers){
userDb.userDao().Delete(currentlyUnknownUser);
}
}
为了使其工作,您必须在Delete(User user)
UserDao.java