【问题标题】:Getting Empty Screen in Firebase Recylerview在 Firebase Recyclerview 中获取空白屏幕
【发布时间】:2018-07-07 09:08:26
【问题描述】:

型号

 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:转换失败 java.lang.Long 类型的值到 String 在 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)

【问题讨论】:

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


    【解决方案1】:

    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;
    }
    ...
    

    【讨论】:

    • 感谢@Frank 。它有效...!!
    【解决方案2】:

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

    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。

    让我们看看结果

    【讨论】:

    • 感谢@Dominic 。它有效...!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2022-06-20
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多