In Android, LayoutInflater is used to convert an xml file (activity_main.xml, fragment_login.xml, gallery_item.xml, etc.) into its coressponding View object.
XML file -> LayoutInflater -> View object in Java
To retrieve an instance of LayoutInflater, we have several options:
Back to your question
Code 1
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
parent: The parent of the current item, in this case, parent is the RecyclerView
RecyclerView is a sub-class of View class, each View will have its own context, in this case, getContext() return the activity where you display the RecyclerView.
Code 2
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
In Android, when initializing an instance of an Adapter, we usually pass a Context instance via a constructor, like this.
class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<Data> data;
public MyAdapter(Context context, List<Data> data) {
this.context = context;
this.data = data;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.gallery_item, parent, false);
return new ViewHolder(itemView);
}
}
In your case, because you do not assign any value to the context, that why the compiler throws that error.
Code 3
context = parent.getContext();
In this case, you don't need to pass a Context via a constructor, just use the context from parent.
class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Data> data;
public MyAdapter(List<Data> data) {
this.data = data;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.gallery_item, parent, false);
return new ViewHolder(itemView);
}
}
Inside onBindViewHolder(), you have 3 options to retrieve an instance of LayoutInflater:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
// Option 1
LayoutInflater inflater = LayoutInflater.from(context);
// Option 2
LayoutInflater inflater2 = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Option 3
LayoutInflater inflater3 = ((Activity) context).getLayoutInflater();
View itemView = inflater.inflate(R.layout.gallery_item, parent, false);
return new ViewHolder(itemView);
}
I always choose option 1 because it's short.