我以前没有使用数据绑定,现在我把它添加到了我的项目中,但是我遇到了一个问题。我不能自动绑定数据。在活动打开期间,我的数据显示在文本位置并立即消失。所以我做了这样的测试,当我点击 card views
在 recyclerview
,我再次绑定。这次我的数据被带到了相关的地方。我有什么问题(对不起,我的英语不好。)
适配器代码;
@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
2条答案
按热度按时间z8dt9xmd1#
我用这种方式更新了代码,错误被修复了。
nwwlzxa72#
尝试
binding.foods = enterFoodsModels
以便将数据类与xml布局连接起来。