微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在自定义对话框中无法使用数据绑定在视图中获取数据-Android

如何解决在自定义对话框中无法使用数据绑定在视图中获取数据-Android

无法使用数据绑定在视图中填充数据。在通常的方法(findViewById)中,数据正在填充,但是在添加绑定数据之后不会出现。

对话课:这是我的对话课

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

使用绑定进行设置时,对话框无法正确显示,并且可以正常方式工作 Xml:这里添加了数据标签

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

我尝试了多种方法,但对我没有用。请建议我这是怎么回事?

解决方法

以下是在xml中声明的变量:

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

为了使用变量“ mapViewModel”,需要在“ executePendingBindings”之前分配它:

mapPopWindowBindig.setVariable(BR.mapViewModel,Object);
mapPopWindowBindig.executePendingByndings();

或直接设置它而不使用'executePendingBinding',如下所示:

mapPopWindowsBinding.setMapViewModel(Object)

如果您在xml中更改变量的名称,它将反映在所有setter方法中

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。