2016-03-30 6 views
3

Kann mir jemand helfen!Bestückung Recyclerview mit Firebase durch Datenbindung

Ich bin ein neuer Entwickler, der diesen Fehler erhält. Ich versuche eine Recyclerview mit Firebase durch Datenbindung in einem Fragment zu bevölkern.

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 enthält die 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> 

Hier ist der Fehler, den ich bekomme.

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)  

es ist die Datenbank, die ich versuche, von Firebase zu holen.

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

}

+0

Es gibt viel zu viel Code und zu viele Bildlaufleisten hier. Wenn Sie Probleme haben, den JSON in eine Java-Klasse zu lesen, schreiben Sie den JSON, die Java-Klasse und den minimalen Code, um zu 'snapshot.getValue (YourClass.call) 'zu gelangen, der den Fehler auslöst. Alle anderen Informationen sind für das Problem irrelevant und stehen im Weg. Siehe [MCVE] (http://stackoverflow.com/help/mcve). –

+0

Crated minimal mit dem gleichen Fehler. Gehen Sie durch das Repo: https://github.com/tusharrai2/ExampleFirebaseDatabinding.git –

+0

Das ist eine ganze App, mit einem Fragment, einer Ansicht, alles. Ich erwarte wirklich, dass dies nichts anderes erfordert, als einen Listener an die richtigen Daten anzuhängen und 'snapshot.getValue (Product.class)' aufzurufen. Wenn Sie es auf das reduzieren können, passt es in Ihre Frage zusammen mit dem JSON (als Text, kein Screenshot). –

Antwort

1

Verwenden @JsonProperty ("Produkt-Beschreibung") auf Ihre Variablen es mit Ihren db entsprechen.

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

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

Das Problem hierbei ist, dass die Jackson-Bibliothek von Firebase Android-Client verwendet zur Serialisierung/Deserialisierung JSON ist in der POJO nicht passenden Eigenschaftsnamen und der DB

+0

Zuvor war meine DB mit "productTitle", "productUnit" und dem gleichen Fehler, den es warf, also änderte ich die db. –

+0

geändert, aber immer noch der gleiche Fehler @Bindable @JsonProperty ("product-unit") public String getProductUnit() {return productUnit; } public void setProductUnit (Zeichenfolge productUnit) {this.productUnit = productUnit; } –

+0

Können Sie das genaue Schema einfügen – Viven

1

Verwenden Sie die @JsonIgnoreProperties (ignoreUnknown = true) auf die Spitze Ihres BaseObservable.class

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

public Product() { 

} 

Refer this link for reference

+0

Hat das aber den gleichen Fehler. –

Verwandte Themen