如何将片段的上下文转换为 LifecycleOwner?
How to transform the context of fragment into a LifecycleOwner?
我有以下情况。我有一个 activity 包含一个片段。在这个片段中,我显示了一些来自后端数据库的记录。我也在使用如下所示的适配器:
public class MovieAdapter extends PagedListAdapter<Movie, MovieAdapter.MovieViewHolder> {
private Context context;
public MovieAdapter(Context context) {this.context = context;}
@NonNull
@Override
public MovieViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//Create the view
}
@Override
public void onBindViewHolder(@NonNull final MovieViewHolder holder, int position) {
Movie movie = getItem(position);
String title = movie.title;
holder.titleTextView.setText(title);
MovieRepository movieRepository = new MovieRepository(context);
LiveData<Movie> liveData = movieRepository.retrieveFavoriteMovie(movie.id);
liveData.observe(context, m -> { //Error
if(m != null) {
boolean favorite = m.favorite;
if(favorite) {
//Do something
} else {
//Do something else
}
}
});
}
class MovieViewHolder extends RecyclerView.ViewHolder {
ImageView favoriteImageView;
TextView titleTextView;
MovieViewHolder(View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.title_text_view); favoriteImageView = itemView.findViewById(R.id.favorite_image_view);
}
}
}
在 onBindViewHolder
中,我试图检查 Romm 数据库中是否存在特定电影,但我收到此错误:
Wrong 1st argument type. Found: 'android.content.Context', required: 'android.arch.lifecycle.LifecycleOwner'
那么如何将片段的上下文转换为 LifecycleOwner
以便我可以在我的方法中将其用作参数?
android.content.Context
没有实现 android.arch.lifecycle.LifecycleOwner
.
您必须传递 AppCompatActivity
的实例,它实现了 android.arch.lifecycle.LifecycleOwner
(或任何其他 class 实现的实例)。
或转换 (AppCompatActivity) context
,当 context
是 instanceof
AppCompatActivity
。要以可靠的方式从上下文中获取 Activity,请检查
为找出上面建议的解决方案的人发布这个,下面我为 kotlin android
添加了 this 的简写扩展版本
private fun Context?.getLifeCycleOwner() : AppCompatActivity? = when {
this is ContextWrapper -> if (this is AppCompatActivity) this else this.baseContext.getLifeCycleOwner()
else -> null
}
片段中上述扩展的用法如下:
private var mainActivityContext: Context? = null //class level variable
//below statement called after context variable assigned / OnCreate
mainActivityContext?.getLifeCycleOwner()?.let { lifeCycleOwner ->
YourViewModel.liveDataReturningMethod(text.toString())
.observe(lifeCycleOwner , Observer { x ->
//your logic here...
})
}
我有以下情况。我有一个 activity 包含一个片段。在这个片段中,我显示了一些来自后端数据库的记录。我也在使用如下所示的适配器:
public class MovieAdapter extends PagedListAdapter<Movie, MovieAdapter.MovieViewHolder> {
private Context context;
public MovieAdapter(Context context) {this.context = context;}
@NonNull
@Override
public MovieViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//Create the view
}
@Override
public void onBindViewHolder(@NonNull final MovieViewHolder holder, int position) {
Movie movie = getItem(position);
String title = movie.title;
holder.titleTextView.setText(title);
MovieRepository movieRepository = new MovieRepository(context);
LiveData<Movie> liveData = movieRepository.retrieveFavoriteMovie(movie.id);
liveData.observe(context, m -> { //Error
if(m != null) {
boolean favorite = m.favorite;
if(favorite) {
//Do something
} else {
//Do something else
}
}
});
}
class MovieViewHolder extends RecyclerView.ViewHolder {
ImageView favoriteImageView;
TextView titleTextView;
MovieViewHolder(View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.title_text_view); favoriteImageView = itemView.findViewById(R.id.favorite_image_view);
}
}
}
在 onBindViewHolder
中,我试图检查 Romm 数据库中是否存在特定电影,但我收到此错误:
Wrong 1st argument type. Found: 'android.content.Context', required: 'android.arch.lifecycle.LifecycleOwner'
那么如何将片段的上下文转换为 LifecycleOwner
以便我可以在我的方法中将其用作参数?
android.content.Context
没有实现 android.arch.lifecycle.LifecycleOwner
.
您必须传递 AppCompatActivity
的实例,它实现了 android.arch.lifecycle.LifecycleOwner
(或任何其他 class 实现的实例)。
或转换 (AppCompatActivity) context
,当 context
是 instanceof
AppCompatActivity
。要以可靠的方式从上下文中获取 Activity,请检查
为找出上面建议的解决方案的人发布这个,下面我为 kotlin android
添加了 this 的简写扩展版本private fun Context?.getLifeCycleOwner() : AppCompatActivity? = when {
this is ContextWrapper -> if (this is AppCompatActivity) this else this.baseContext.getLifeCycleOwner()
else -> null
}
片段中上述扩展的用法如下:
private var mainActivityContext: Context? = null //class level variable
//below statement called after context variable assigned / OnCreate
mainActivityContext?.getLifeCycleOwner()?.let { lifeCycleOwner ->
YourViewModel.liveDataReturningMethod(text.toString())
.observe(lifeCycleOwner , Observer { x ->
//your logic here...
})
}