Android studio,数据绑定在打开 activity 时不起作用

Android studio, databinding does not work on opening the activity

我以前没有使用数据绑定,现在我将它添加到我的项目中,但我遇到了问题。我无法自动绑定我的数据。在 activity 打开期间,我的数据显示在文本位置并立即消失。所以我为了测试目的做了这样的事情,当我点击 recyclerview 中的 card views 时,我再次绑定。这次我的数据已经被带到了相关的地方。我的问题是什么? (我很抱歉我的英语不好。)

适配器代码;

 @NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    FoodListBinding binding = DataBindingUtil.inflate(inflater,R.layout.food_list,parent,false);
    return new ViewHolder(binding);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
    holder.binding.foodNameTextview.setText(_foodModelArrayList.get(position).getBesin_adi());
    holder.binding.portionTextview.setText(_foodModelArrayList.get(position).getBesin_id());

    holder.foodListCardview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.binding.foodNameTextview.setText(_foodModelArrayList.get(position).getBesin_adi());
            holder.binding.portionTextview.setText(_foodModelArrayList.get(position).getBesin_id());
        }
    });
}

@Override
public int getItemCount() {
    return _foodModelArrayList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    FoodListBinding binding;
    private LottieAnimationView addFoodAnim;
    private CardView foodListCardview;

    public ViewHolder(@NonNull FoodListBinding binding) {
        super(binding.getRoot());
        this.binding = binding;
    }
}

片段代码;

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    Intent getDateFromActivity = getActivity().getIntent();
    date = getDateFromActivity.getStringExtra("date");
    loadingAnim = view.findViewById(R.id.loadingAnim);

    recyclerView = view.findViewById(R.id.recyclerView);


    enterFoodViewModel.getFood().observe(requireActivity(), new Observer<List<EnterFoodModel>>() {
        @Override
        public void onChanged(List<EnterFoodModel> enterFoodModels) {
            if (enterFoodModels!=null){
                loadingAnim.setVisibility(View.GONE);
            }
            foodList = enterFoodModels;
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            enterFoodRecyclerAdapter = new EnterFoodRecyclerAdapter(getContext(),foodList,date);
            recyclerView.setAdapter(enterFoodRecyclerAdapter);
            enterFoodRecyclerAdapter.notifyDataSetChanged();
        }
    });
}

xml代码;

<?xml version="1.0" encoding="utf-8"?>
<layout>
  <data>
   <variable
     name="foods"
     type="com.example.calorie.models.EnterFoodModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp">
<androidx.cardview.widget.CardView
    android:id="@+id/foodListCardview"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="2dp"
    android:clickable="true"
    android:elevation="10dp"
    android:focusable="true"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardCornerRadius="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:id="@+id/foodNameTextview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:gravity="center"
    android:text="@{foods.besin_adi}"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
<TextView
    android:id="@+id/portionTextview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:textColor="@color/colorAccent"
    android:text="@{foods.besin_id}"
    android:textSize="15sp"
    app:layout_constraintStart_toStartOf="@+id/foodNameTextview"
    app:layout_constraintTop_toBottomOf="@+id/foodNameTextview" />
<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/addFoodAnim"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginEnd="16dp"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:lottie_autoPlay="true"
    app:lottie_loop="true"
    app:lottie_rawRes="@raw/loading" />
  </androidx.constraintlayout.widget.ConstraintLayout>
  </androidx.cardview.widget.CardView>
  </androidx.constraintlayout.widget.ConstraintLayout>
</layout>


Problem images;
When the applications is fragment: https://hizliresim.com/7HA2E8
After clicking on cardviews: https://hizliresim.com/qu8yIO

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

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
    EnterFoodModel enterFoodModel = _foodModelArrayList.get(position);
    holder.bind(enterFoodModel);
}

public class ViewHolder extends RecyclerView.ViewHolder {
    FoodListBinding binding;
public ViewHolder(FoodListBinding binding) {
    super(binding.getRoot());
    this.binding = binding;
}
public void bind(EnterFoodModel enterFoodModel){
    binding.setFoods(enterFoodModel);
    binding.executePendingBindings();
    }
}

尝试 binding.foods = enterFoodsModels 以便将您的数据 class 与 XML 布局连接起来。