简体   繁体   中英

Unable to get data in view using data-binding in custom dialog - Android

Unable to populate data in view using data binding. In normal method(findViewById), Data is populating but after added binding data is not appear.

Dialog Class: This is my dialog class

public class MapPopUpWindow {
    MapPopWindowBinding mapPopWindowBinding;
    String locationName;
    String restaurantName;

    public void DialogMenu(Context context, Bundle bundle) {
        getDataFromBundle(bundle);
        Dialog dialog = new Dialog(context, R.style.DialogTheme);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mapPopWindowBinding = (MapPopWindowBinding) DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.map_pop_window, null, false);
        dialog.setContentView(mapPopWindowBinding.getRoot());
        mapPopWindowBinding.executePendingBindings();
        Objects.requireNonNull(dialog.getWindow()).setGravity(Gravity.BOTTOM);
        dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.show();
    }

    private void getDataFromBundle(Bundle bundle){
        locationName = bundle.getString("location_name");
        restaurantName = bundle.getString("restaurant_name");
    }

    public String setLocationName(){
        return locationName;
    }

    public String setRestaurantName(){
        return restaurantName;
    }
}

Dialog is not properly showing when set using binding and it works with normal way Xml : Here added data tag

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <import type="android.view.View" />
        <variable
            name="mapView"
            type="de.iteratec.mywork.utils.MapPopUpWindow" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">

        <Button
            android:id="@+id/close_btn"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="20dp"
            android:background="@drawable/icon_clock_green"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/location_name"
            android:layout_width="100dp"
            android:textStyle="bold"
            android:textColor="@color/petrol_70pct"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="20dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:text="@{mapViewModel.setLocationName()}"/>

        <TextView
            android:id="@+id/restaurant_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="5dp"
            android:textStyle="bold"
            android:textColor="@color/black"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/location_name"
            android:text="@{mapViewModel.setRestaurantName()}"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

I have tried multiple ways but didn't work for me. Please suggest me what is wrong in it?

Unable to populate data in view using data binding. In normal method(findViewById), Data is populating but after added binding data is not appear.

Dialog Class: This is my dialog class

public class MapPopUpWindow {
    MapPopWindowBinding mapPopWindowBinding;
    String locationName;
    String restaurantName;

    public void DialogMenu(Context context, Bundle bundle) {
        getDataFromBundle(bundle);
        Dialog dialog = new Dialog(context, R.style.DialogTheme);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mapPopWindowBinding = (MapPopWindowBinding) DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.map_pop_window, null, false);
        dialog.setContentView(mapPopWindowBinding.getRoot());
        mapPopWindowBinding.executePendingBindings();
        Objects.requireNonNull(dialog.getWindow()).setGravity(Gravity.BOTTOM);
        dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.show();
    }

    private void getDataFromBundle(Bundle bundle){
        locationName = bundle.getString("location_name");
        restaurantName = bundle.getString("restaurant_name");
    }

    public String setLocationName(){
        return locationName;
    }

    public String setRestaurantName(){
        return restaurantName;
    }
}

Dialog is not properly showing when set using binding and it works with normal way Xml : Here added data tag

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <import type="android.view.View" />
        <variable
            name="mapView"
            type="de.iteratec.mywork.utils.MapPopUpWindow" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">

        <Button
            android:id="@+id/close_btn"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="20dp"
            android:background="@drawable/icon_clock_green"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/location_name"
            android:layout_width="100dp"
            android:textStyle="bold"
            android:textColor="@color/petrol_70pct"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="20dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:text="@{mapViewModel.setLocationName()}"/>

        <TextView
            android:id="@+id/restaurant_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="5dp"
            android:textStyle="bold"
            android:textColor="@color/black"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/location_name"
            android:text="@{mapViewModel.setRestaurantName()}"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

I have tried multiple ways but didn't work for me. Please suggest me what is wrong in it?

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