通过数据绑定使用firebase填充recyclerview

时间:2016-03-30 11:28:25

标签: firebase firebase-realtime-database

任何人都可以帮助我!

我是一个新的开发人员收到此错误。我试图通过片段中的数据绑定来填充带有firebase的recyclerview。

ProductListFragment.java

public class ProductListFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    RecyclerView recyclerView = (RecyclerView) inflater.inflate(R.layout.recycler_view, container, false);
    FirebaseRecyclerAdapter adapter;
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    Firebase mFirebaseRef = new Firebase(Constants.FIREBASE_URL).child("subcategories").child("abc");

     adapter = new FirebaseRecyclerAdapter<Product, ViewHolder>(
            Product.class, R.layout.fragment_list_product, ViewHolder.class, mFirebaseRef) {
        @Override
        protected void populateViewHolder(ViewHolder viewHolder, Product product, int i) {

            FragmentProductListBinding binding = viewHolder.getBinding();

            binding.setProduct(product);
        }
    };
    recyclerView.setAdapter(adapter);
    return recyclerView;
}

public static class ViewHolder extends RecyclerView.ViewHolder {

    FragmentProductListBinding binding;

    public ViewHolder (View rootView) {
        super(rootView);
        binding = DataBindingUtil.bind(rootView);
    }
    public FragmentProductListBinding getBinding() {
        return binding;
    }
}

}

Product.java包含POJO

public class Product extends BaseObservable {

public String productTitle;
public String productUnit;
public String productMRP;
public String productSellingPrice;
public String productDescription;

public Product() {

}

@Bindable
public String getProductUnit() {
    return productUnit;
}

public void setProductUnit(String productUnit) {
    this.productUnit = productUnit;
}

@Bindable
public String getProductMRP() {
    return productMRP;
}

public void setProductMRP(String productMRP) {
    this.productMRP = productMRP;
}

@Bindable
public String getProductSellingPrice() {
    return productSellingPrice;
}

public void setProductSellingPrice(String productSellingPrice) {
    this.productSellingPrice = productSellingPrice;
}

@Bindable
public String getProductDescription() {
    return productDescription;
}

public void setProductDescription(String productDescription) {
    this.productDescription = productDescription;
}

@Bindable
public String getProductTitle() {
    return productTitle;
}

public void setProductTitle(String productTitle) {
    this.productTitle = productTitle;
}

}

fragment_list_product.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
    <variable name="Product" type="com.abc.www.abc.model.Product"/>
</data>

<RelativeLayout
    android:padding="@dimen/activity_horizontal_margin"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/product_image"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_twitter"
        android:contentDescription="@string/product_list_image_content_description"/>

    <TextView
        android:id="@+id/product_mrp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="@{Product.productMRP}"
        android:textAppearance="?attr/textAppearanceListItem"
        android:textSize="14sp"
        android:layout_gravity="center"
        android:layout_below="@+id/product_title"
        android:layout_toRightOf="@+id/product_image"
        android:layout_toEndOf="@+id/product_image" />

    <TextView
        android:id="@+id/product_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_toRightOf="@+id/product_image"
        android:layout_toEndOf="@+id/product_image"
        android:layout_marginEnd="16dp"
        android:text="@{Product.productTitle}"
        android:textAppearance="?attr/textAppearanceListItem"
        android:textSize="16sp"
        android:layout_gravity="top" />

    <TextView
        android:id="@+id/product_selling_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="@{Product.productSellingPrice}"
        android:textAppearance="?attr/textAppearanceListItem"
        android:textSize="14sp"
        android:layout_gravity="center"
        android:layout_below="@+id/product_title"
        android:layout_toRightOf="@+id/product_mrp"
        android:layout_toEndOf="@+id/product_mrp" />

</RelativeLayout>

这是我得到的错误。

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.abc.www.abc, PID: 1894
                                                                             com.firebase.client.FirebaseException: Failed to bounce to type
                                                                                 at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
                                                                                 at com.firebase.ui.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:161)
                                                                                 at com.firebase.ui.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:150)
                                                                                 at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:190)
                                                                                 at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5453)
                                                                                 at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5486)
                                                                                 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4723)
                                                                                 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4599)
                                                                                 at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1988)
                                                                                 at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384)
                                                                                 at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347)
                                                                                 at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
                                                                                 at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3003)
                                                                                 at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2881)
                                                                                 at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1457)
                                                                                 at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:147)
                                                                                 at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:285)
                                                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                                 at android.view.Choreographer.doFrame(Choreographer.java:603)
                                                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5422)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                              Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.abc.www.abc.model.Product] from String value; no single-String constructor/factory method
                                                                                 at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:428)
                                                                                 at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:299)
                                                                                 at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1056)
                                                                                 at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:136)
                                                                                 at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
                                                                                 at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
                                                                                 at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
                                                                                 at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)
                                                                                 at com.firebase.ui.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:161) 
                                                                                 at com.firebase.ui.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:150) 
                                                                                 at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:190) 
                                                                                 at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5453) 
                                                                                 at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5486) 
                                                                                 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4723) 
                                                                                 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4599) 
                                                                                 at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1988) 
                                                                                 at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384) 
                                                                                 at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347) 
                                                                                 at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574) 
                                                                                 at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3003) 
                                                                                 at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2881) 
                                                                                 at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1457) 
                                                                                 at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:147) 
                                                                                 at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:285) 
                                                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
                                                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
                                                                                 at android.view.Choreographer.doFrame(Choreographer.java:603) 
                                                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
                                                                                 at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5422) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

这是我试图从firebase获取的数据库。

subcategories{
             abc{
                 product-description:"xxadasccdcnsdcnsdc"
                 product-mrp: 100
                 product-selling-price: 80
                 product-title:"Name"
                 product-unit: "100 meter"}

}

2 个答案:

答案 0 :(得分:1)

在变量之上使用@JsonProperty(“product-description”)将其与数据库匹配。

@JsonProperty("product-title")
public String productTitle;

@JsonProperty("product-unit")
public String productUnit;

问题在于Firebase Android客户端用于序列化/反序列化JSON的Jackson库与POJO和DB中的属性名称不匹配

答案 1 :(得分:1)

在BaseObservable.class顶部使用@JsonIgnoreProperties(ignoreUnknown = true)

@JsonIgnoreProperties(ignoreUnknown = true)
public class Product extends BaseObservable {

public Product() {

}

Refer this link for reference