I have added an adapter to my recycler view, the adapter contains the data plist which is an ArrayList populated with firebase data. I have done this in the onCreateView method in my Fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_buy, container, false);
recyler = rootView.findViewById(R.id.recycler);
recyler.setHasFixedSize(true);
reference = FirebaseDatabase.getInstance().getReference("Produce");
plist = new ArrayList<>();
adapter = new Produce_RecyclerViewAdapter(rootView.getContext(), plist);
recyler.setAdapter(adapter);
final int count = 0;
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()) {
Produce produce = ds.getValue(Produce.class);
plist.add(produce);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Display();
}
});
return inflater.inflate(R.layout.fragment_buy, container, false);
As far as I can tell there are no errors with my Recycler adapter:
public class Produce_RecyclerViewAdapter extends RecyclerView.Adapter<Produce_RecyclerViewAdapter.ViewHolder>{
private final List<Produce> produce_item;
private final Context context;
public Produce_RecyclerViewAdapter(Context context, ArrayList<Produce> produce_item) {
this.produce_item = produce_item;
this.context = context;
}
@NonNull
@NotNull
@Override
public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.produce_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
Produce produce = produce_item.get(position);
holder.ptitle.setText(produce.getPname());
holder.pprice.setText(produce.getPprice());
Picasso.with(context)
.load(produce.getPimg())
.placeholder(R.mipmap.ic_launcher)
.fit()
.centerCrop()
.into(holder.pimage);
}
@Override
public int getItemCount() {
return produce_item.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView pimage;
TextView ptitle, pprice;
CardView view, addtocart;
public ViewHolder(@NonNull @NotNull View itemView) {
super(itemView);
pimage = itemView.findViewById(R.id.pimage);
ptitle = itemView.findViewById(R.id.ptitle);
pprice = itemView.findViewById(R.id.pprice);
view = itemView.findViewById(R.id.view);
addtocart = itemView.findViewById(R.id.addtocart);
}
}
}
I added the linear layout manager directly to the recycler view:
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
and here is the firebase data: firebase data
Edit: I checked out recyclerview No adapter attached; skipping layout and quite a few other answers relating to the problem but either I have already done what they suggested or they are referring to recycler view in an activity and not in a fragment.
CodePudding user response:
The No adapter attached; skipping layout
is harmless and overall can be ignored.
Your problem is that you are inflating the view two times. You inflate first and setup everything, throw that way. Inflate it second time and return it without any setup.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_buy, container, false);
/*
* All of the setup
*/
//return inflater.inflate(R.layout.fragment_buy, container, false);
return rootView;
}