在Firebase Recylerview中获取空白屏幕

时间:2018-07-07 09:08:26

标签: android firebase-realtime-database android-recyclerview recycler-adapter

Firebase Data Picture

模型

 private String name,rating,ImageUrl,description,maxminutes,minorder,minquaninty;

    public Try_Model() {

    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRating() {
        return rating;
    }

    public void setRating(String rating) {
        this.rating = rating;
    }

    public String getImageUrl() {
        return ImageUrl;
    }

    public void setImageUrl(String imageUrl) {
        ImageUrl = imageUrl;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getMaxminutes() {
        return maxminutes;
    }

    public void setMaxminutes(String maxminutes) {
        this.maxminutes = maxminutes;
    }

    public String getMinorder() {
        return minorder;
    }

    public void setMinorder(String minorder) {
        this.minorder = minorder;
    }

    public String getMinquaninty() {
        return minquaninty;
    }

    public void setMinquaninty(String minquaninty) {
        this.minquaninty = minquaninty;
    }
}

MainActivity

    mRecycleriew =findViewById(R.id.my_recycler_view);
    mRecycleriew.setLayoutManager(new LinearLayoutManager(this));



    mFirebaseDatabase=FirebaseDatabase.getInstance();
    mRef=mFirebaseDatabase.getReference().child("restaurants");


}


protected void onStart() {

    super.onStart();
    FirebaseRecyclerAdapter<Try_Model,ViewHolders>firebaseRecyclerAdapter=
            new FirebaseRecyclerAdapter<Try_Model, ViewHolders>(
                    Try_Model.class,
                    R.layout.shop_name_list,
                    ViewHolders.class,
                    mRef)
            {
                @Override
                protected void populateViewHolder(ViewHolders viewHolder, Try_Model model, int position) {


                    viewHolder.setDetails(getApplicationContext(),model.getName(),model.getDescription(),model.getImageUrl(),model.getMaxminutes(),model.getMinorder(),model.getMinquaninty(),model.getRating());

                }
            };
           mRecycleriew.setAdapter(firebaseRecyclerAdapter);
}

ViewHolders

 View mView;

public ViewHolders(View itemView) {

    super(itemView);
    mView=itemView;
}
String name,rating,ImageUrl,description,maxminutes,minorder,minquaninty;
public  void setDetails(Context ctx, String modelName, String name, String description, String ImageUrl, String maxminutes, String minorder, String minquaninty){

    TextView mName=mView.findViewById(R.id.shopTitle);
    TextView mRating=mView.findViewById(R.id.shoprating);
    TextView mDescription=mView.findViewById(R.id.shopdescrpt);
    TextView mMinorder=mView.findViewById(R.id.shopminorder);
    TextView mMaxminutes=mView.findViewById(R.id.shopmaxminutes);
    TextView mMaxQuantity=mView.findViewById(R.id.shopquantityperperson);
    ImageView mImageView = mView.findViewById(R.id.shopimageView);


    mName.setText(name);
    mDescription.setText(description);
    mMaxminutes.setText(maxminutes);
    mMinorder.setText(minorder);
    mMaxQuantity.setText(minquaninty);
    mRating.setText(rating);
    Picasso.with(mView.getContext()).load(ImageUrl).placeholder( R.drawable.progress_animation ).into(mImageView);

}

错误详细信息

  

com.google.firebase.database.DatabaseException:转换失败   String的java.lang.Long类型的值           在com.google.android.gms.internal.firebase_database.zzkt.zzb(未知   资源)           位于com.google.android.gms.internal.firebase_database.zzkt.zza(未知   资源)           在com.google.android.gms.internal.firebase_database.zzkt.zzb(未知   资源)           在com.google.android.gms.internal.firebase_database.zzku.zza(未知   资源)           在com.google.android.gms.internal.firebase_database.zzkt.zzb(未知   资源)           位于com.google.android.gms.internal.firebase_database.zzkt.zza(未知   资源)           位于com.google.firebase.database.DataSnapshot.getValue(未知来源)           在com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:151)           在com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:140)           在com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:183)           在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:6673)           在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:6714)           在android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)           在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)           在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5752)           在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5748)           在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2232)           在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)           在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)           在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)           在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)           在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)           在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)           在android.view.View.layout(View.java:16653)           在android.view.ViewGroup.layout(ViewGroup.java:5438)           在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)           在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)           在android.widget.LinearLayout.onLayout(LinearLayout.java:1495)           在android.view.View.layout(View.java:16653)

2 个答案:

答案 0 :(得分:2)

您的rating属性值存储为数字,但是您的代码将其声明为字符串。要修复

private String name,ImageUrl,description,maxminutes,minorder,minquaninty;
private Double rating;

public Try_Model() {

}
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Double getRating() {
    return rating;
}

public void setRating(Double rating) {
    this.rating = rating;
}
...

答案 1 :(得分:0)

错误就在错误日志的第一行上

com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String

尝试将此mRating.setText(rating);更改为此mRating.setText(rating.toString());或此mRating.setText(String.valueOf(rating));

,还将模型类中rating的数据类型更改为double或Double。

让我们看一下结果