Android DataBinding:我可以将构造函数参数传递给我的数据变量吗?
Android DataBinding: can I pass constructor parameter to my data variable?
我有一个 MovieViewModel
class 在我的布局文件中用作变量。
<data>
<variable
name="vm"
type="udacity.nanodegree.android.p2.model.comum.MovieViewModel"/>;
</data>
A special variable named context is generated for use in binding
expressions as needed. The value for context is the Context from the
root View's getContext(). The context variable will be overridden by
an explicit variable declaration with that name.
我需要将这个特殊变量 context
传递给我的 class 最好由布局文件中的构造函数传递:
public class MovieViewModel {
private Context context;
public MovieViewModel(Context context) {
this.context = context;
}
}
有办法吗?
如果不可能,我想把它传递给我的属性,比如:
<TextView
android:id="@+id/text_release"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="2dp"
android:text="@{vm.releaseDateFormated(context) ?? ``}"
tools:text="0000"/>
要在 MovieViewModel
中使用上下文,如下所示:
public String getReleaseDateFormated (Context context){
return releaseDate == null? null: context.getString(R.string.dateToYear, releaseDate);
根据这个 我可以这样做,但我尝试并收到错误:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:cannot find method releaseDateFormated(android.content.Context) in class udacity.nanodegree.android.p2.model.comum.MovieViewModel
file:C:\Users\alexandre\Udacity\Nanodegrees\AndroidDevelopment\P2-PopularMovies\app\src\main\res\layout\fragment_detail.xml
loc:74:40 - 74:70
****\ data binding error ****
"Is there a way to do that?
数据绑定框架未创建 MovieViewModel
的实例。因此,构造函数参数是您的工作,当 您 创建 MovieViewModel
.
的实例时
I tried and received the error
您的错误不是来自问题中的代码。错误指的是releaseDateFormated()
.
我有一个 MovieViewModel
class 在我的布局文件中用作变量。
<data>
<variable
name="vm"
type="udacity.nanodegree.android.p2.model.comum.MovieViewModel"/>;
</data>
A special variable named context is generated for use in binding expressions as needed. The value for context is the Context from the root View's getContext(). The context variable will be overridden by an explicit variable declaration with that name.
我需要将这个特殊变量 context
传递给我的 class 最好由布局文件中的构造函数传递:
public class MovieViewModel {
private Context context;
public MovieViewModel(Context context) {
this.context = context;
}
}
有办法吗?
如果不可能,我想把它传递给我的属性,比如:
<TextView
android:id="@+id/text_release"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="2dp"
android:text="@{vm.releaseDateFormated(context) ?? ``}"
tools:text="0000"/>
要在 MovieViewModel
中使用上下文,如下所示:
public String getReleaseDateFormated (Context context){
return releaseDate == null? null: context.getString(R.string.dateToYear, releaseDate);
根据这个
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:cannot find method releaseDateFormated(android.content.Context) in class udacity.nanodegree.android.p2.model.comum.MovieViewModel
file:C:\Users\alexandre\Udacity\Nanodegrees\AndroidDevelopment\P2-PopularMovies\app\src\main\res\layout\fragment_detail.xml
loc:74:40 - 74:70
****\ data binding error ****
"Is there a way to do that?
数据绑定框架未创建 MovieViewModel
的实例。因此,构造函数参数是您的工作,当 您 创建 MovieViewModel
.
I tried and received the error
您的错误不是来自问题中的代码。错误指的是releaseDateFormated()
.