Z Irani
Z Irani

Reputation: 35

Problem in RecyclerView Adapter AndroidStudio

i use Android Studio Version 3.6 with API 29 and i has problem in the recycler View Adapter in this code

@NonNull
@Override
public MyView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(getContext()).inflate(R.layout.exampel,parent,false);

    return new MyView(view);
}

why i cant use the getContext()??

Upvotes: 0

Views: 144

Answers (2)

Agustin Pazos
Agustin Pazos

Reputation: 131

RecyclerView.Adapter does not have a getContext() you need to use the parent context.

@NonNull
@Override
public MyView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.exampel,parent,false);
 return new MyView(view);
}

Upvotes: 0

Rick Sanchez
Rick Sanchez

Reputation: 4756

Because RecyclerView.Adapter doesn't have a getContext() method. You can use parent.getContext() instead

Upvotes: 1

Related Questions