I have the following scenario. I have an activity which holds a fragment. In this fragment I'm displaying some records from a back-end database. I'm also using an adapter that looks like this:
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);
}
}
}
In the onBindViewHolder
I'm trying to check if a specific movie exist in Romm database but I get this error:
Wrong 1st argument type. Found: 'android.content.Context', required: 'android.arch.lifecycle.LifecycleOwner'
So how to transform the context of fragment into a LifecycleOwner
so I can use it as in argument in my method?
Every Activity has a Lifecycle. This Activity will be a Lifecycle Owner(any activity or fragment can be called a lifecycle owner). When an activity is initialized LifecycleOwner is the one that will be constructed initially. Any class that implements the LifeCycleOwner interface indicates that it has Android LifeCycle.
When using a ViewModel exposing LiveData s for a Fragment , there are currently two LifecycleOwners that can be used to oberve: the Fragment itself or. the Fragment 's property mViewLifecycleOwner .
ProcessLifecycleOwner. Class that provides lifecycle for the whole application process. A class that has an Android lifecycle. These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment.
Android provides built-in components that are lifecycle owners. For activities, ComponentActivity , which is the base class for AppCompatActivity , is the one that implements LifecycleOwner . However, there are other classes that already implement this interface, too. For example, Fragment is a LifecycleOwner .
android.content.Context
does not implement android.arch.lifecycle.LifecycleOwner
.
You'd have to pass an instance of AppCompatActivity
, which implements android.arch.lifecycle.LifecycleOwner
(or any other class which does that).
or cast (AppCompatActivity) context
, when context
is an instanceof
AppCompatActivity
. To get the Activity from the Context in a reliable manner, check https://stackoverflow.com/a/46205896/2413303
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With