1

still very new at java android and I wanna make a recycler view on a fragment. Found an online tutorial on YouTube and I try to recreate the code. But instead of placing the recycler on my main activity I put it on my fragment which is in my main activity. Also I only include some part which I think is the root of my error.

The code on my fragment file

list = new ArrayList<>();
    myAdapter =  new adapter1(this, list);
    recyclerView.setAdapter(myAdapter);

The code on my adapter

public class adapter1 extends RecyclerView.Adapter<adapter1.MyViewHolder> {


Context context;

ArrayList<itemFile> list;

public adapter1(Context context, ArrayList<itemFile> list) {
    this.context = context;
    this.list = list;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
    return new MyViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

    itemFile user = list.get(position);
    holder.itemName.setText(user.getItemName());
    holder.price.setText(user.getItemPrice());

}

it says that Menu cannot be converted into Context

Bochiii
  • 13
  • 5

1 Answers1

0

For fragment use getContext() instead of this as an argument to your adapter.

 list = new ArrayList<>();
myAdapter =  new adapter1(getContext(), list);
recyclerView.setAdapter(myAdapter);
Danish
  • 676
  • 5
  • 10