Populate recyclerview with firebase through databinding
谁能帮我!
我是一个新的开发人员收到此错误。 我试图通过片段中的数据绑定来填充带有firebase的recyclerview。
ProductListFragment.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | <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> |
这是我得到的错误。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | 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获取的数据库。
1 2 3 4 5 6 7 | subcategories{ abc{ product-description:"xxadasccdcnsdcnsdc" product-mrp: 100 product-selling-price: 80 product-title:"Name" product-unit:"100 meter"} |
}
&NBSP;
在BaseObservable.class的顶部使用@JsonIgnoreProperties(ignoreUnknown = true)
1 2 3 4 5 6 | @JsonIgnoreProperties(ignoreUnknown = true) public class Product extends BaseObservable { public Product() { } |
请参阅此链接以供参考
在变量之上使用@JsonProperty("product-description")将其与db匹配。
1 2 3 4 5 | @JsonProperty("product-title") public String productTitle; @JsonProperty("product-unit") public String productUnit; |
这里的问题是Firebase Android客户端用于序列化/反序列化JSON的Jackson库与POJO和DB中的属性名称不匹配