RecyclerView启动时将显示所有数据,而不是显示特定ID的数据。我使用的是Room数据库,据我了解,问题在于,当ViewModel初始化时,存储库将获取所有颜色。我无法弄清楚如何在存储库中编码,因此在初始化ViewModel时,它仅获取具有特定ID的数据,而不是所有数据。
在堆栈的答案之一中,它提到了有关使用“ SwitchMap”的一些内容...但是我是Android的新手,因此不确定是否应该使用它以及应该如何使用它。
下面是我的相关代码如下:
片段代码:
'''
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recyclerViewColors = getActivity().findViewById(R.id.recyclerViewColors);
recyclerViewColors.setHasFixedSize(true);
layoutManagerColors = new LinearLayoutManager(getActivity()); //This is the code for LinearLayoutManager
recyclerViewColors.setLayoutManager(layoutManagerColors);
mAdapterColors = new MyAdapterColors(colors); //MyAdapter is a Recycler.Adapter class
recyclerViewColors.setAdapter(mAdapterColors);
addColor = getActivity().findViewById(R.id.addColor);
addColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
if(id != -1){
bundle.putInt("ID",id);
}
Navigation.findNavController(getActivity().findViewById(R.id.editLinear)).navigate(R.id.action_editStatusFragment_to_addColors3, bundle);
}
});
edit_text_titleedit = getActivity().findViewById(R.id.edit_text_titleedit);
edit_text_descriptionedit = getActivity().findViewById(R.id.edit_text_descriptionedit);
edit_text_titleedit.setText(title);
edit_text_descriptionedit.setText(description);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
mViewModel = new ViewModelProvider(this).get(FragmentStatusViewModel.class);
mViewModel.loadComments(id).observe(getViewLifecycleOwner(), new Observer<List<Colors>>() {
@Override
public void onChanged(List<Colors> colors) {
// Toast.makeText(getActivity(), "size is = " + colors.size() + " & "
// + id, Toast.LENGTH_SHORT).show();
mAdapterColors.setColors(colors);
}
});
if (getArguments() != null && getArguments().containsKey("IDAdd")){
String colorname = getArguments().getString("colorname");
String fabricquality = getArguments().getString("fabricquality");
String productionunitname = getArguments().getString("productionunitname");
String millname = getArguments().getString("millname");
int statusid = getArguments().getInt("IDAdd");
Colors colors = new Colors(colorname,fabricquality,productionunitname,millname, statusid);
mViewModel.insert(colors);
//mViewModel.getAllStyles(statusid);
Toast.makeText(getActivity(), "Status id = " + statusid, Toast.LENGTH_LONG).show();
}else {
//Toast.makeText(getActivity(), "id is " + id, Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), "Colors displayed!", Toast.LENGTH_SHORT).show();
}
}
'''
适配器代码:
'''
public class MyAdapterColors extends RecyclerView.Adapter<MyViewHolderColors> {
private List<Colors> colors;
public MyAdapterColors(List<Colors> colors) {
this.colors = colors;
}
@NonNull
@Override
public MyViewHolderColors onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.colorsrow, parent, false);
MyViewHolderColors myViewHolderColors = new MyViewHolderColors(view);
return myViewHolderColors;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolderColors holder, int position) {
Colors color = colors.get(position);
holder.colorname.setText(color.getColorname().toString());
holder.fabricquality.setText(color.getFabricQuality());
holder.productionunitname.setText(color.getProductionUnitName());
holder.fabricmill.setText(color.getFabricMill());
}
@Override
public int getItemCount() {
return colors.size();
}
public void setColors(List<Colors> newcolors) {
this.colors = newcolors;
notifyDataSetChanged();
}
public Colors getColorsAt(int position) {
return colors.get(position);
}
}
'''ViewModel代码
'''
public class FragmentStatusViewModel extends AndroidViewModel {
private Status_Repository repository;
private MutableLiveData<Integer> id;
List<Colors> colors;
private LiveData<List<Status>> allStatus;
private LiveData<List<Colors>> allColors;
private LiveData<List<Stylewithcolors>> stylewithcolors;
public FragmentStatusViewModel(@NonNull Application application) {
super(application);
repository = new Status_Repository(application);
allStatus = repository.getAllStatus();
}
LiveData<List<Colors>> loadComments(int statusId){
allColors = repository.loadComments(statusId);
return allColors;
}
'''我的存储库代码:''' 公共类Status_Repository {
private StatusDao statusDao;
private LiveData<List<Status>> allStatus;
List<Colors> colors;
private LiveData<List<Colors>> allColors;
// private ColorsDao colorsDao;
// private LiveData<List<Colors>> allColors;
//private List<Colors> colors;
private LiveData<List<Stylewithcolors>> allStyles;
public Status_Repository(Application application) {
Status_Database database = Status_Database.getInstance(application);
statusDao = database.statusDao();
allStatus = statusDao.getAllStatus();
allColors = statusDao.getAllColors();
// colorsDao = database.colorsDao();
// allColors = colorsDao.getAllColors();
}
LiveData<List<Colors>> loadComments(int statusId){
Status_Database.databaseWriteExecutor.execute(() -> {
allColors = statusDao.loadComments(statusId);
});
return allColors;
}
'''道查询:
'''
@Query("SELECT * FROM colors where statusid = :statusId")
LiveData<List<Colors>> loadComments(int statusId);
'''
第二个片段的XML代码
'''
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/editLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context=".UpdateFragment">
<EditText
android:id="@+id/edit_text_titleedit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title"
android:inputType="text" />
<EditText
android:id="@+id/edit_text_descriptionedit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Description"
android:inputType="textMultiLine" />
</LinearLayout>
<Button
android:id="@+id/addColor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="Add Color"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/editLinear" />
<androidx.cardview.widget.CardView
android:id="@+id/cardRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/addColor">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewColors"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
'''
我通过直接从Fragment访问存储库并跳过ViewModel解决了这个问题。现在,RecyclerView仅显示所选ID的详细信息。
它解决了我的问题,但我不知道它是否会引起任何问题,或者它是否是解决问题的正确方法...现在,我不明白,如果在会议室数据库的情况下可以直接从Fragment访问存储库,为什么我们应该使用ViewModel。
现在我的存储库代码和片段代码如下所示
'''
Status_Repository repository = new Status_Repository(getActivity().getApplication(), id);
//MyViewModelTest myViewModel = new ViewModelProvider(this, new MyViewModelFactory(getActivity().getApplication(), id)).get(MyViewModelTest.class);
repository.loadComments(id).observe(getViewLifecycleOwner(), new Observer<List<Colors>>() {
@Override
public void onChanged(List<Colors> colors) {
Toast.makeText(getActivity(), "size is = " + colors.size() + " & "
+ id, Toast.LENGTH_SHORT).show();
mAdapterColors.setColors(colors);
}
});
'''仓库代码
'''
public Status_Repository(Application application, int id) {
Status_Database database = Status_Database.getInstance(application);
statusDao = database.statusDao();
allStatus = statusDao.getAllStatus();
allColors = statusDao.loadComments(id);
// colorsDao = database.colorsDao();
// allColors = colorsDao.getAllColors();
}
'''