Java 无法使用自定义对话框中的数据绑定获取视图中的数据-Android
无法使用数据绑定填充视图中的数据。在普通方法(findViewById)中,数据正在填充,但添加绑定后,数据不会出现 对话类:这是我的对话类Java 无法使用自定义对话框中的数据绑定获取视图中的数据-Android,java,android,data-binding,dialog,Java,Android,Data Binding,Dialog,无法使用数据绑定填充视图中的数据。在普通方法(findViewById)中,数据正在填充,但添加绑定后,数据不会出现 对话类:这是我的对话类 public class MapPopUpWindow { MapPopWindowBinding mapPopWindowBinding; String locationName; String restaurantName; public void DialogMenu(Context context, Bundle
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>
或者直接设置它而不使用“executePendingBinding”,如下所示:
mapPopWindowsBinding.setMapViewModel(Object)
如果更改xml中变量的名称,它将反映在所有setter方法中我没有使用视图模型。好的,但变量的名称是,无论如何,它没有很好的逻辑
mapPopWindowsBinding.setMapViewModel(Object)