android studio,数据绑定在打开活动时不起作用

apeeds0o  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(426)

我以前没有使用数据绑定,现在我把它添加到了我的项目中,但是我遇到了一个问题。我不能自动绑定数据。在活动打开期间,我的数据显示在文本位置并立即消失。所以我做了这样的测试,当我点击 card viewsrecyclerview ,我再次绑定。这次我的数据被带到了相关的地方。我有什么问题(对不起,我的英语不好。)
适配器代码;

  1. @NonNull
  2. @Override
  3. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  4. LayoutInflater inflater = LayoutInflater.from(parent.getContext());
  5. FoodListBinding binding = DataBindingUtil.inflate(inflater,R.layout.food_list,parent,false);
  6. return new ViewHolder(binding);
  7. }
  8. @Override
  9. public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
  10. holder.binding.foodNameTextview.setText(_foodModelArrayList.get(position).getBesin_adi());
  11. holder.binding.portionTextview.setText(_foodModelArrayList.get(position).getBesin_id());
  12. holder.foodListCardview.setOnClickListener(new View.OnClickListener() {
  13. @Override
  14. public void onClick(View v) {
  15. holder.binding.foodNameTextview.setText(_foodModelArrayList.get(position).getBesin_adi());
  16. holder.binding.portionTextview.setText(_foodModelArrayList.get(position).getBesin_id());
  17. }
  18. });
  19. }
  20. @Override
  21. public int getItemCount() {
  22. return _foodModelArrayList.size();
  23. }
  24. public class ViewHolder extends RecyclerView.ViewHolder {
  25. FoodListBinding binding;
  26. private LottieAnimationView addFoodAnim;
  27. private CardView foodListCardview;
  28. public ViewHolder(@NonNull FoodListBinding binding) {
  29. super(binding.getRoot());
  30. this.binding = binding;
  31. }
  32. }

片段代码;

  1. @Override
  2. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  3. super.onViewCreated(view, savedInstanceState);
  4. Intent getDateFromActivity = getActivity().getIntent();
  5. date = getDateFromActivity.getStringExtra("date");
  6. loadingAnim = view.findViewById(R.id.loadingAnim);
  7. recyclerView = view.findViewById(R.id.recyclerView);
  8. enterFoodViewModel.getFood().observe(requireActivity(), new Observer<List<EnterFoodModel>>() {
  9. @Override
  10. public void onChanged(List<EnterFoodModel> enterFoodModels) {
  11. if (enterFoodModels!=null){
  12. loadingAnim.setVisibility(View.GONE);
  13. }
  14. foodList = enterFoodModels;
  15. recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
  16. enterFoodRecyclerAdapter = new EnterFoodRecyclerAdapter(getContext(),foodList,date);
  17. recyclerView.setAdapter(enterFoodRecyclerAdapter);
  18. enterFoodRecyclerAdapter.notifyDataSetChanged();
  19. }
  20. });
  21. }

xml代码;

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layout>
  3. <data>
  4. <variable
  5. name="foods"
  6. type="com.example.calorie.models.EnterFoodModel" />
  7. </data>
  8. <androidx.constraintlayout.widget.ConstraintLayout
  9. xmlns:android="http://schemas.android.com/apk/res/android"
  10. xmlns:app="http://schemas.android.com/apk/res-auto"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:layout_marginTop="4dp">
  14. <androidx.cardview.widget.CardView
  15. android:id="@+id/foodListCardview"
  16. android:layout_width="match_parent"
  17. android:layout_height="80dp"
  18. android:layout_marginStart="8dp"
  19. android:layout_marginEnd="8dp"
  20. android:layout_marginBottom="2dp"
  21. android:clickable="true"
  22. android:elevation="10dp"
  23. android:focusable="true"
  24. android:foreground="?android:attr/selectableItemBackground"
  25. app:cardCornerRadius="8dp"
  26. app:layout_constraintBottom_toBottomOf="parent"
  27. app:layout_constraintEnd_toEndOf="parent"
  28. app:layout_constraintStart_toStartOf="parent"
  29. app:layout_constraintTop_toTopOf="parent">
  30. <androidx.constraintlayout.widget.ConstraintLayout
  31. android:layout_width="match_parent"
  32. android:layout_height="match_parent">
  33. <TextView
  34. android:id="@+id/foodNameTextview"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_marginStart="16dp"
  38. android:layout_marginTop="16dp"
  39. android:gravity="center"
  40. android:text="@{foods.besin_adi}"
  41. android:textColor="@android:color/black"
  42. android:textSize="18sp"
  43. app:layout_constraintStart_toStartOf="parent"
  44. app:layout_constraintTop_toTopOf="parent" />
  45. <TextView
  46. android:id="@+id/portionTextview"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:layout_marginTop="8dp"
  50. android:gravity="center"
  51. android:textColor="@color/colorAccent"
  52. android:text="@{foods.besin_id}"
  53. android:textSize="15sp"
  54. app:layout_constraintStart_toStartOf="@+id/foodNameTextview"
  55. app:layout_constraintTop_toBottomOf="@+id/foodNameTextview" />
  56. <com.airbnb.lottie.LottieAnimationView
  57. android:id="@+id/addFoodAnim"
  58. android:layout_width="50dp"
  59. android:layout_height="50dp"
  60. android:layout_marginEnd="16dp"
  61. android:visibility="gone"
  62. app:layout_constraintBottom_toBottomOf="parent"
  63. app:layout_constraintEnd_toEndOf="parent"
  64. app:layout_constraintTop_toTopOf="parent"
  65. app:lottie_autoPlay="true"
  66. app:lottie_loop="true"
  67. app:lottie_rawRes="@raw/loading" />
  68. </androidx.constraintlayout.widget.ConstraintLayout>
  69. </androidx.cardview.widget.CardView>
  70. </androidx.constraintlayout.widget.ConstraintLayout>
  71. </layout>
  72. Problem images;
  73. When the applications is fragment: https://hizliresim.com/7HA2E8
  74. After clicking on cardviews: https://hizliresim.com/qu8yIO
z8dt9xmd

z8dt9xmd1#

我用这种方式更新了代码,错误被修复了。

  1. @Override
  2. public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
  3. EnterFoodModel enterFoodModel = _foodModelArrayList.get(position);
  4. holder.bind(enterFoodModel);
  5. }
  6. public class ViewHolder extends RecyclerView.ViewHolder {
  7. FoodListBinding binding;
  8. public ViewHolder(FoodListBinding binding) {
  9. super(binding.getRoot());
  10. this.binding = binding;
  11. }
  12. public void bind(EnterFoodModel enterFoodModel){
  13. binding.setFoods(enterFoodModel);
  14. binding.executePendingBindings();
  15. }
  16. }
展开查看全部
nwwlzxa7

nwwlzxa72#

尝试 binding.foods = enterFoodsModels 以便将数据类与xml布局连接起来。

相关问题