2016-11-19 51 views
0

上找到我是一個新手,我試圖添加一些數據到來自firbase db.recyclerview但它顯示不可序列化的錯誤。我到處搜尋,發現夫婦的答案,使類的靜態和變量公開,但仍無法正常工作的,這裏是我的代碼FireBasedb無類序列化在類

我的適配器

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

回答

0

看起來像你有錯誤的順序下面的行?

adapterl = new CardViewAdapter(mFirebaseDatabaseReference.child(MENU_CHILD)); 
    mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference().child("menu");