我是新手,我正在尝试将一些数据添加到来自firbase db的recyclerview中。但它显示了不可序列化的错误。我到处搜索,找到几个答案,使类静态和变量公开,但仍然无法正常工作,这里是我的代码
我的适配器
public class CardViewAdapter extends FirebaseRecyclerAdapter<MainpageCardModel,CardViewAdapter.Recyclerviewholder>{
public Context mcontext;
public List<MainpageCardModel> cakeslist;
public CardViewAdapter(DatabaseReference ref){
super(MainpageCardModel.class,R.layout.card,CardViewAdapter.Recyclerviewholder.cl ass,ref);
}
@Override
protected void populateViewHolder(Recyclerviewholder viewHolder, MainpageCardModel model, int position) {
viewHolder.ItemName.setText(MainpageCardModel.getItemName());
viewHolder.ItemPrice.setText(MainpageCardModel.getItemPrice());
if(MainpageCardModel.getItemImage() == null){
viewHolder.CardviewImageView.setImageDrawable(ContextCompat.getDrawable(mcontext,
R.drawable.ic_cake_dark));
}
else {
Glide.with(mcontext)
.load(MainpageCardModel.getItemImage())
.into(viewHolder.CardviewImageView);
}
}
public static class Recyclerviewholder extends RecyclerView.ViewHolder {
public ImageView CardviewImageView;
public TextView ItemName, ItemPrice;
public Recyclerviewholder(View itemView) {
super(itemView);
CardviewImageView = (ImageView) itemView.findViewById(R.id.cakepic_cardview);
ItemName = (TextView) itemView.findViewById(R.id.caketitle_cardview);
ItemPrice = (TextView) itemView.findViewById(R.id.cakeprice_cardview);
}
}
我的模特课
public Class MainpageCardModel{
public static String itemName;
public static String itemImage;
public static String itemPrice;
public MainpageCardModel(){
}
public MainpageCardModel(String itemName, String itemImage, String itemPrice){
this.itemImage=itemImage;
this.itemName=itemName;
this.itemPrice=itemPrice;
}
public static String getItemName() {
return itemName;
}
public void setItemName(String itemName){
this.itemName = itemName;
}
public static String getItemImage(){
return itemImage;
}
public void setItemImage(String itemImage){
this.itemImage= itemImage;
}
public static String getItemPrice(){
return itemPrice;
}
public void setItemPrice(String itemPrice) {
this.itemPrice = itemPrice;
}
}
我的片段
private static final String TAG = "FreshCreamFragment";
public static final String MENU_CHILD = "FreshCream";
private String Cakename, cakeprice, cakeurl;
private RecyclerView mRecyclerView;
private DatabaseReference mFirebaseDatabaseReference;
private CardViewAdapter adapterl;
private GridLayoutManager gridLayoutManager;
public List<MainpageCardModel> cakeslist;
ProgressBar mprogressbar;
MainpageCardModel mainpageCardModel;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cakeslist= new ArrayList<>();
adapterl = new CardViewAdapter(mFirebaseDatabaseReference.child(MENU_CHILD));
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference().child("menu");
mFirebaseDatabaseReference.child(MENU_CHILD).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String,String> cakedetails = (Map)dataSnapshot.getValue();
Cakename= cakedetails.get("Item_name");
cakeprice = cakedetails.get("Item_price");
cakeurl = cakedetails.get("url");
mainpageCardModel = new MainpageCardModel(Cakename,cakeurl,cakeprice);
cakeslist.add(mainpageCardModel);
adapterl.notifyDataSetChanged();
mprogressbar.setVisibility(ProgressBar.INVISIBLE);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fresh_cream_fragment, container, false);
mprogressbar = (ProgressBar) rootview.findViewById(R.id.progressBar3);
mRecyclerView = (RecyclerView) rootview.findViewById(R.id.freshcream_recyclerview);
gridLayoutManager = new GridLayoutManager(getContext(),2);
mRecyclerView.setLayoutManager(gridLayoutManager);
mRecyclerView.setAdapter(adapterl);
return rootview;
}
}
MYDB mydbscreenshot
答案 0 :(得分:0)
看起来你的行有错误的顺序?
adapterl = new CardViewAdapter(mFirebaseDatabaseReference.child(MENU_CHILD));
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference().child("menu");