Home > Software engineering >  Firebase in Recycler view in fragment
Firebase in Recycler view in fragment

Time:10-18

So I was wanting to load Firebase real-time database in a Recycler-view in a fragment. Here is my fragment:

public class buy extends Fragment {    
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private RecyclerView recyler;
    private DatabaseReference reference;
    private ArrayList<Produce> plist;
    private Produce_RecyclerViewAdapter adapter;

    public buy() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static buy newInstance(String param1, String param2) {
        buy fragment = new buy();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @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);
        recyler.setLayoutManager(new LinearLayoutManager(rootView.getContext()));
        reference = FirebaseDatabase.getInstance().getReference("Produce");

        plist = new ArrayList<>();
        adapter = new Produce_RecyclerViewAdapter(plist, rootView.getContext());
        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);


    }
    private void Display() {
        Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
    }

}

Here is my adapter class:

public class Produce_RecyclerViewAdapter extends RecyclerView.Adapter<Produce_RecyclerViewAdapter.ViewHolder>{
    private List<Produce> produce_item;
    private Context context;

    public Produce_RecyclerViewAdapter(List<Produce> produce_item, Context context) {
        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);

        }
    }

}

Here is my Produce.java:

public class Produce {    
    String pname;
    String pdesc;
    String pimg;
    String pfarm;
    String pprice;
    String pcategory;
    boolean instock;

    public Produce(){

    }

    public Produce(String pname, String pdesc, String pimg, String pfarm, boolean instock, String pprice, String pcategory){
        this.pname = pname;
        this.pdesc = pdesc;
        this.pimg = pimg;
        this.pfarm = pfarm;
        this.instock = instock;
        this.pprice = pprice;
        this.pcategory = pcategory;
    }

    public String getPname(){
        return pname;
    }
    public String getPdesc(){
        return pdesc;
    }
    public String getPimg(){
        return pimg;
    }
    public String getPfarm() {
        return pfarm;
    }
    public boolean isInstock() {
        return instock;
    }
    public String getPprice(){return pprice;}
    public String getPcategory(){return pcategory;}
    public void setPimg(String pimg){this.pimg = pimg;}
}

Here is my data:

image

even though I added setter to pimg I get: Stack trace: No setter/field for pimage found on class com.example.belko_fresh.Produce what do I do?

edit: after editing the firebase data to match my Produce class I get the stack trace: No adapter attached; skipping layout.

CodePudding user response:

The database has a property called pimage, while your Java class has a field called pimg. Since the two names don't match, the Firebase SDK can't automatically map between them and raises an error.

To remove the error, ensure your Java code and database use the same names for properties, i.e.:

public String getPimage(){
    return pimg;
}
...
public void setPimage(String pimg){this.pimg = pimg;}
  • Related