简体   繁体   中英

Android studio, databinding does not work on opening the activity

I was not using databinding before and I added it to my project now, but I have a problem. I cannot automatically bind my data. During the activity opening, my data is shown in text places and disappears immediately. So I did something like this for testing purposes, when I clicked on the card views in recyclerview , I bind again. This time my data has been taken to the relevant places. What is my problem? (I'm sorry for my bad english.)

Adapter Code;

 @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;
    }
}

Fragment Code;

@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 code;

<?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

I updated my codes this way and the error was fixed.

@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();
    }
}

Try binding.foods = enterFoodsModels in order to connect your data class with the XML layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM