android-fragments 片段中回收器视图中的Firebase

ffx8fchx  于 2022-11-13  发布在  Android
关注(0)|答案(1)|浏览(184)

所以我想在一个片段中的Recycler视图中加载Firebase实时数据库。

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();
    }

}

下面是我的适配器类:

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);

        }
    }

}

这里是我的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;}
}

以下是我的数据:

即使我在pimg中添加了setter,我也会得到:堆栈追踪:在类com.example.belko_fresh. produce上找不到pimage的setter/字段我该怎么办?
编辑:在编辑firebase数据以匹配我的Produce类之后,我得到了堆栈跟踪:未连接适配器;跳过布局。

kmbjn2e3

kmbjn2e31#

数据库有一个名为pimage的属性,而您的Java类有一个名为pimg的字段。由于这两个名称不匹配,Firebase SDK无法在它们之间自动Map,并引发错误。
要删除此错误,请确保Java代码与数据库使用相同得属性名称,即:

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

相关问题