java - 在同一个回收站视图中显示两个实时数据列表
问题描述
我试过的:我认为这个问题适合使用 MediatorLiveData,但是我不确定如何将它应用到我的代码中,因为我在网上发现的用法主要用于 Kotlin。目前,我正在尝试将 MVVM 方案与 2 个实体(BPcheck、MEDtaken)及其对应的 Dao 和 View Models、一个存储库、一个数据库和一个 RecyclerViewAdapter 一起使用。最初,我尝试创建 Recording 的 BPcheck 和 MEDtaken 子类,但是我遇到了类型转换的问题。接下来我尝试让每个单独的类,但后来遇到了不理解在同一个 recyclerview 中显示两个 LiveData 列表的问题。
目标:我试图在同一个 RecyclerView 中显示来自两个 LiveData 列表的数据,并根据数据类型膨胀不同的 Recyclerview 布局。将来我想在这个 Recyclerview 中添加更多的 LiveData 列表,每个列表都有自己的布局。
背景:我一直在关注以下视频系列来帮助创建应用程序:https ://www.youtube.com/watch?v=Jwdty9jQN0E&ab_channel=CodinginFlow
我的代码:
public class RecordingsRecyclerViewAdapter extends RecyclerView.Adapter{
final int VIEW_TYPE_MEDTAKEN = 0;
final int VIEW_TYPE_BPCHECK = 1;
private List<MedTaken> medTakenList = new ArrayList<>();
private List<BPcheck> bpChecksList = new ArrayList<>();
// !! Ideally, this recordingsList would hold both lists. !!//
private List<Object> recordingsList = new ArrayList<>();
private Context recordings_context;
public RecordingsRecyclerViewAdapter(Context recordings_context) {
this.recordings_context = recordings_context;
}
@Override
// My Attempt at trying to get different datatypes to inflate different layouts
public int getItemViewType(int position) {
if (recordingsList.get(position) instanceof BPcheck){
return 1;
}else{
return 0;
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view;
if (viewType == VIEW_TYPE_BPCHECK) {
view = layoutInflater.inflate(R.layout.recordings_bloodpressurecheck_item, parent, false);
return new ViewHolder_BPCHECK(view);
}
if(viewType==VIEW_TYPE_MEDTAKEN) {
view = layoutInflater.inflate(R.layout.recordings_medtaken_item, parent, false);
return new ViewHolder_MEDTAKEN(view);
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if(holder.getItemViewType()==1){
//bind viewholder2
ViewHolder_BPCHECK viewHolder_bpcheck = (ViewHolder_BPCHECK) holder;
viewHolder_bpcheck.systolic.setText(((BPcheck) recordingsList.get(position)).getSystolicBP());
}else{
ViewHolder_MEDTAKEN viewHolder_medtaken = (ViewHolder_MEDTAKEN) holder;
viewHolder_medtaken.subtitle.setText(((MedTaken) recordingsList.get(position)).getMedication().getMedCommonName());
}
}
@Override
public int getItemCount() {
return recordingsList.size();
}
class ViewHolder_MEDTAKEN extends RecyclerView.ViewHolder{
TextView datetime,subtitle;
public ViewHolder_MEDTAKEN(@NonNull View itemView) {
super(itemView);
datetime = itemView.findViewById(R.id.txt_recording_datetime);
subtitle = itemView.findViewById(R.id.txt_recording_subtitle);
}
}
class ViewHolder_BPCHECK extends RecyclerView.ViewHolder{
TextView datetime,systolic,diastolic;
public ViewHolder_BPCHECK(@NonNull View itemView) {
super(itemView);
datetime = itemView.findViewById(R.id.txt_bpCheck_datetime);
systolic = itemView.findViewById(R.id.txt_systolic);
diastolic = itemView.findViewById(R.id.txt_diastolic);
}
}
public class AllRecordingsActivity extends AppCompatActivity {
// !! RecordingViewModel is not created, but I just wanted to give an idea of where I was trying to go with this
private RecordingViewModel recordingViewModel;
private RecyclerView recordingsRecyclerView;
private RecordingsRecyclerViewAdapter recordingsRecyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_recordings);
recordingsRecyclerView = findViewById(R.id.recordingsRecyclerView);
recordingsRecyclerViewAdapter = new RecordingsRecyclerViewAdapter(this);
recordingsRecyclerView.setAdapter(recordingsRecyclerViewAdapter);
recordingsRecyclerView.setLayoutManager(new GridLayoutManager(this,1));
// !! not really sure what to do here
recordingViewModel = new ViewModelProvider(this).get(RecordingViewModel.class);
recordingViewModel.getAllRecordings().observe(this, new Observer<List<Recording>>() {
@Override
public void onChanged(List<Recording> recordings) {
recordingsRecyclerViewAdapter.setRecordingsList(recordings);
}
});
}
public class PatientRepository {
private MedicationDao medicationDao;
private BPcheckDao bpCheckDao;
private LiveData<List<Medication>> allMedications;
private LiveData<List<MedTaken>> allMedTaken;
private LiveData<List<BPcheck>> allBPchecks;
public PatientRepository(Application application){
PatientDatabase database = PatientDatabase.getInstance(application);
medTakenDao= database.medTakenDao();
allMedTaken = medTakenDao.getAllMedTaken();
bpCheckDao= database.bpCheckDao();
allBPchecks = bpCheckDao.getAllBPchecks();
}
// MedTaken Methods
public void addToMedTaken(MedTaken medTaken){
new InsertMedTakenAsyncTask(medTakenDao).execute(medTaken);
}
public void updateMedTaken(MedTaken medTaken){
new UpdateMedTakenAsyncTask(medTakenDao).execute(medTaken);
}
public void deleteMedTaken(MedTaken medTaken){
new DeleteMedTakenAsyncTask(medTakenDao).execute(medTaken);
}
public LiveData<List<MedTaken>> getAllMedTaken() {
return allMedTaken;
}
// MedTaken ASYNC Tasks
private static class InsertMedTakenAsyncTask extends AsyncTask<MedTaken,Void,Void>{
private MedTakenDao medTakenDao;
private InsertMedTakenAsyncTask(MedTakenDao medTakenDao){
this.medTakenDao = medTakenDao;
}
@Override
protected Void doInBackground(MedTaken... medTaken) {
medTakenDao.insert(medTaken[0]);
return null;
}
}
private static class UpdateMedTakenAsyncTask extends AsyncTask<MedTaken,Void,Void>{
private MedTakenDao medTakenDao;
private UpdateMedTakenAsyncTask(MedTakenDao medTakenDao){
this.medTakenDao = medTakenDao;
}
@Override
protected Void doInBackground(MedTaken... medTaken) {
medTakenDao.update(medTaken[0]);
return null;
}
}
private static class DeleteMedTakenAsyncTask extends AsyncTask<MedTaken,Void,Void>{
private MedTakenDao medTakenDao;
private DeleteMedTakenAsyncTask(MedTakenDao medTakenDao){
this.medTakenDao = medTakenDao;
}
@Override
protected Void doInBackground(MedTaken... medTaken) {
medTakenDao.delete(medTaken[0]);
return null;
}
}
//BP Checks methods
public void addToBPchecks(BPcheck bpCheck){
new InsertBPchecksAsyncTask(bpCheckDao).execute(bpCheck);
}
public void updateBPchecks(BPcheck bpCheck){
new UpdateBPchecksAsyncTask(bpCheckDao).execute(bpCheck);
}
public void deleteBPchecks(BPcheck bpCheck){
new DeleteBPchecksAsyncTask(bpCheckDao).execute(bpCheck);
}
public LiveData<List<BPcheck>> getAllBPchecks() {
return allBPchecks;
}
// BPChecks ASYNC Tasks
private static class InsertBPchecksAsyncTask extends AsyncTask<BPcheck,Void,Void>{
private BPcheckDao bpCheckDao;
private InsertBPchecksAsyncTask(BPcheckDao bpCheckDao){
this.bpCheckDao = bpCheckDao;
}
@Override
protected Void doInBackground(BPcheck... bpChecks) {
bpCheckDao.insert(bpChecks[0]);
return null;
}
}
private static class UpdateBPchecksAsyncTask extends AsyncTask<BPcheck,Void,Void>{
private BPcheckDao bpCheckDao;
private UpdateBPchecksAsyncTask(BPcheckDao bpCheckDao){
this.bpCheckDao = bpCheckDao;
}
@Override
protected Void doInBackground(BPcheck... bpChecks) {
bpCheckDao.update(bpChecks[0]);
return null;
}
}
private static class DeleteBPchecksAsyncTask extends AsyncTask<BPcheck,Void,Void>{
private BPcheckDao bpCheckDao;
private DeleteBPchecksAsyncTask(BPcheckDao bpCheckDao){
this.bpCheckDao = bpCheckDao;
}
@Override
protected Void doInBackground(BPcheck... bpChecks) {
bpCheckDao.delete(bpChecks[0]);
return null;
}
}
@Entity(tableName = "BPcheck_table")
public class BPcheck {
@PrimaryKey(autoGenerate = true)
private int id;
private String date;
private String timestamp;
private int systolicBP;
private int diastolicBP;
private String comments;
public BPcheck(String date, String timestamp, int systolicBP, int diastolicBP, String comments) {
this.date = date;
this.timestamp = timestamp;
this.systolicBP = systolicBP;
this.diastolicBP = diastolicBP;
this.comments = comments;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public int getSystolicBP() {
return systolicBP;
}
public void setSystolicBP(int systolicBP) {
this.systolicBP = systolicBP;
}
public int getDiastolicBP() {
return diastolicBP;
}
public void setDiastolicBP(int diastolicBP) {
this.diastolicBP = diastolicBP;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
}
@Dao
public interface BPcheckDao {
@Insert
void insert(BPcheck BPcheck);
@Update
void update(BPcheck BPcheck);
@Delete
void delete(BPcheck BPcheck);
@Query("SELECT * FROM BPcheck_table")
LiveData<List<BPcheck>> getAllBPchecks();
}
public class RecordingsViewModel extends AndroidViewModel {
private PatientRepository repository;
private LiveData<List<MedTaken>> allMedTaken;
private LiveData<List<BPcheck>> allBPChecks;
private MediatorLiveData allRecordings;
public RecordingsViewModel(@NonNull Application application) {
super(application);
repository = new PatientRepository(application);
}
public LiveData<List<Object>> getAllRecordings() {
return allRecordings;
}
}
@Entity(tableName = "MedTaken_table")
public class MedTaken {
private int id;
private Medication medication;
private String comments;
private String date;
private String timestamp;
public MedTaken(Medication medication, String comments, String date, String timestamp) {
this.medication = medication;
this.comments = comments;
this.date = date;
this.timestamp = timestamp;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Medication getMedication() {
return medication;
}
public void setMedication(Medication medication) {
this.medication = medication;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
@Dao
public interface MedTakenDao {
@Insert
void insert(MedTaken medTaken);
@Update
void update(MedTaken medTaken);
@Delete
void delete(MedTaken medTaken);
@Query("SELECT * FROM MedTaken_table")
LiveData<List<MedTaken>> getAllMedTaken();
}
public class MedTakenViewModel extends AndroidViewModel {
private PatientRepository repository;
private LiveData<List<MedTaken>> allMedTaken;
public MedTakenViewModel(@NonNull Application application) {
super(application);
repository = new PatientRepository(application);
allMedTaken = repository.getAllMedTaken();
}
public void insert(MedTaken medTaken){ repository.addToMedTaken(medTaken); }
public void update(MedTaken medTaken){ repository.updateMedTaken(medTaken); }
public void delete(MedTaken medTaken){ repository.deleteMedTaken(medTaken); }
public LiveData<List<MedTaken>> getAllMedTaken() {
return allMedTaken;
}
}
解决方案
推荐阅读
- python - 如何在特定字符串之后找到第一次出现的数字+单词组合?
- javascript - 条纹:ERR_BLOCKED_BY_RESPONSE
- kubernetes - 在 NFS 中设置 PVC,不会挂载设置的 PVC 大小,而是设置整个 NFS 卷的大小
- android - 如何在 Android 11 中启用快速访问钱包 UI
- trace32 - Trace32 - 从内存中读取 ASCII 到宏/变量中
- python - xArray 箭袋图失败
- vue.js - 将计算方法获取到另一个组件(Vuejs)
- python - 体素操作旋转、翻转?
- django - 如何在 Django 中加入嵌套查询
- regression - 如何提高随机森林回归预测结果