[英]Unable to get data in view using data-binding in custom dialog - 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>
我尝试了多种方法,但对我不起作用。 请建议我有什么问题?
无法使用数据绑定在视图中填充数据。 在普通方法(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>
我尝试了多种方法,但对我不起作用。 请建议我有什么问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.