Populate recyclerview with firebase through databinding

Can anyone help me!

i am a new developer getting this error. I am trying to populate a recyclerview with firebase through databinding in a fragment.

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 contains the 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>

here is the error which i am getting.

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) 

it is the database which i am try to fetch from firebase.

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

}

 

Answers


Use @JsonProperty("product-description") on top of your variables to match it with your db.

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

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

The problem here is that the Jackson library used by Firebase Android client to serialize/deserialize JSON is not matching property names in the POJO and the DB


Use the @JsonIgnoreProperties(ignoreUnknown = true) on the top of your BaseObservable.class

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

public Product() {

}

Refer this link for reference


Need Your Help

Problems writing to a file

java android file-io ioexception fileoutputstream

I am trying to write to a file in an Android app with this code:

format in ZXing?

android barcode zxing

I am just wondering what is the format in Zxing.