Android Studio、データバインディングはアクティビティを開くときに機能しません

user13669147

以前はデータバインディングを使用していなかったので、今はプロジェクトに追加しましたが、問題があります。データを自動的にバインドできません。アクティビティの開始時に、データはテキストの場所に表示され、すぐに消えます。そこで、テスト目的でこのようなことをしました。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 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
イワン・ガルザ

binding.foods = enterFoodsModelsデータクラスをXMLレイアウトに接続してみてください

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

アクティビティを開くことができません:Androidビューモデルのインスタンスを作成できません

分類Dev

Androidオブザーバーパターンとデータバインディング、ブールプロパティが変更されたときにアクティビティを開始します

分類Dev

アクションバーのアクティビティがAndroidで開始されると、フラグメントが機能しませんか?

分類Dev

アクションバーのアクティビティがAndroidで開始されると、フラグメントが機能しませんか?

分類Dev

Android:メインクラスがアクションバーアクティビティに拡張されたときにエラー(java.lang.RuntimeException:アクティビティComponentInfoを開始できません)が発生しました

分類Dev

Androidは、新しいインスタンスを作成せずにアクティビティからフラグメントにデータを渡します

分類Dev

データバインディングは、ユーザーがダークモードをアクティブ化/非アクティブ化したために強制的に更新された場合にのみ、Android10で機能します

分類Dev

Androidのアクティビティからフラグメントへのデータの受け渡しが機能していません

分類Dev

Androidのデータバインディングが正しく機能していません

分類Dev

Androidデータバインディングがビュー 'android:tag'プロパティで機能しない

分類Dev

Androidはプログラムでランチャーアクティビティを開始できません

分類Dev

Android Studioで新しいアクティビティを開くことができません

分類Dev

Androidがlistviewsetadapterでアクティビティnullポインター例外を開始できません

分類Dev

Android:RecyclerViewのアイテムはデータバインディングとレルムを介して更新されません

分類Dev

Androidアプリへのディープリンクを有効にしようとすると、テストインテントがアクティビティを起動できません

分類Dev

compileSdkVersion 23を使用したAPK、APK22を使用してAndroid携帯でメインアクティビティを開くことができません

分類Dev

android:データを失うことなくメインアクティビティに戻る

分類Dev

Android:ボタンをクリックしてもアクティビティを開始できません

分類Dev

Android Studioにユーザーがログインした後、さまざまなアクティビティでアプリを開く方法

分類Dev

Android Studioは3.1.0アップデート後にデータバインディングを生成できません

分類Dev

AndroidデータバインディングとLiveData:LiveDataプロパティの値にバインドできません

分類Dev

アクティビティを開始できませんandroid.view.InflateException:バイナリXMLファイルの行#11:クラスフラグメントのインフレーション中にエラーが発生しました

分類Dev

アクティビティを開始できませんComponentInfo {Activity}:android.view.InflateException:バイナリXMLファイルの行#12:クラスフラグメントのインフレーション中にエラーが発生しました

分類Dev

Androidのディープリンクは常に最初にブラウザで開き、次に正しいアクティビティが開きます

分類Dev

タブ付きアクティビティの最初のタブに切り替えると、Androidで正しく機能しません

分類Dev

android-ViewPagerのアクティビティからフラグメントにデータを渡します

分類Dev

Android Studio-タブ付きアクティビティフラグメントをデフォルトビューにリセットする方法は?

分類Dev

アクティビティにボタンを置くとAndroidアプリケーションがクラッシュし、空のアクティビティが機能します

分類Dev

開いているアクティビティを閉じて、メインアクティビティがAndroidで状態を失うことなくフラグメントを更新できるようにします

Related 関連記事

  1. 1

    アクティビティを開くことができません:Androidビューモデルのインスタンスを作成できません

  2. 2

    Androidオブザーバーパターンとデータバインディング、ブールプロパティが変更されたときにアクティビティを開始します

  3. 3

    アクションバーのアクティビティがAndroidで開始されると、フラグメントが機能しませんか?

  4. 4

    アクションバーのアクティビティがAndroidで開始されると、フラグメントが機能しませんか?

  5. 5

    Android:メインクラスがアクションバーアクティビティに拡張されたときにエラー(java.lang.RuntimeException:アクティビティComponentInfoを開始できません)が発生しました

  6. 6

    Androidは、新しいインスタンスを作成せずにアクティビティからフラグメントにデータを渡します

  7. 7

    データバインディングは、ユーザーがダークモードをアクティブ化/非アクティブ化したために強制的に更新された場合にのみ、Android10で機能します

  8. 8

    Androidのアクティビティからフラグメントへのデータの受け渡しが機能していません

  9. 9

    Androidのデータバインディングが正しく機能していません

  10. 10

    Androidデータバインディングがビュー 'android:tag'プロパティで機能しない

  11. 11

    Androidはプログラムでランチャーアクティビティを開始できません

  12. 12

    Android Studioで新しいアクティビティを開くことができません

  13. 13

    Androidがlistviewsetadapterでアクティビティnullポインター例外を開始できません

  14. 14

    Android:RecyclerViewのアイテムはデータバインディングとレルムを介して更新されません

  15. 15

    Androidアプリへのディープリンクを有効にしようとすると、テストインテントがアクティビティを起動できません

  16. 16

    compileSdkVersion 23を使用したAPK、APK22を使用してAndroid携帯でメインアクティビティを開くことができません

  17. 17

    android:データを失うことなくメインアクティビティに戻る

  18. 18

    Android:ボタンをクリックしてもアクティビティを開始できません

  19. 19

    Android Studioにユーザーがログインした後、さまざまなアクティビティでアプリを開く方法

  20. 20

    Android Studioは3.1.0アップデート後にデータバインディングを生成できません

  21. 21

    AndroidデータバインディングとLiveData:LiveDataプロパティの値にバインドできません

  22. 22

    アクティビティを開始できませんandroid.view.InflateException:バイナリXMLファイルの行#11:クラスフラグメントのインフレーション中にエラーが発生しました

  23. 23

    アクティビティを開始できませんComponentInfo {Activity}:android.view.InflateException:バイナリXMLファイルの行#12:クラスフラグメントのインフレーション中にエラーが発生しました

  24. 24

    Androidのディープリンクは常に最初にブラウザで開き、次に正しいアクティビティが開きます

  25. 25

    タブ付きアクティビティの最初のタブに切り替えると、Androidで正しく機能しません

  26. 26

    android-ViewPagerのアクティビティからフラグメントにデータを渡します

  27. 27

    Android Studio-タブ付きアクティビティフラグメントをデフォルトビューにリセットする方法は?

  28. 28

    アクティビティにボタンを置くとAndroidアプリケーションがクラッシュし、空のアクティビティが機能します

  29. 29

    開いているアクティビティを閉じて、メインアクティビティがAndroidで状態を失うことなくフラグメントを更新できるようにします

ホットタグ

アーカイブ