我的问题
01-04 18:05:36.912 2828-2828/com.p1519536gmail.recipies E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.p1519536gmail.recipies, PID: 2828
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type Model.Ingredients
at com.google.android.gms.internal.zzdzr.zzb(Unknown Source:842)
at com.google.android.gms.internal.zzdzr.zza(Unknown Source:0)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:22)
at com.firebase.ui.database.ObservableSnapshotArray.getObject(ObservableSnapshotArray.java:160)
at com.firebase.ui.database.CachingObservableSnapshotArray.getObject(CachingObservableSnapshotArray.java:40)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:180)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:217)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1710)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:346)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:655)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
user_id /cat_id
//The user_id
final String cat_id = getRef(position).getKey();
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent profileIntent = new Intent(MainActivity.this, SingleRecipie.class);
profileIntent.putExtra("Category_id",cat_id);
startActivity(profileIntent);
}
});
片段XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="Fragment.recipie">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/cardView">
<ImageView
android:id="@+id/recipie_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/recipie_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/overlayBackground"
android:gravity="center"
android:text="Name of menu"
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/ingredients_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/cardView" />
</RelativeLayout>
我的类别的模型,mRecipieDatabase=FirebaseDatabase. getInstance().getApplication().child("类别")
package Model;
/**
* Created by Keiren on 1/4/2018.
*/
public class Category {
private String Name;
private String Image;
//Constructor
public Category() {
}
public Category(String Name, String Image) {
this.Name = Name;
this.Image = Image;
}
//Getter and Setter
public String getName() {return Name;}
public void setName(String Name) { this.Name = Name;}
public String getImage() {
return Image;
}
public void setImage(String Image) {this.Image = Image;
}
}
食品模型,mDatabase=FirebaseDatabase. getInstance().getApplication().child("食品");
package Model;
/**
* Created by Keiren on 1/4/2018.
*/
public class Ingredients {
private String x;
public Ingredients() {
}
public Ingredients(String x) {
this.x = x;
}
public String getX() {return x;}
public void setX(String x) {
this.x = x;
}
}
我不知道我的DataSnapshot是否与我的Firebase Recycler View冲突,我对编码还很陌生,所以我有点迷茫,因为我不知道去哪里看,这是我第一次使用片段编码,所以它有点混淆功能
package Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.p1519536gmail.recipies.MainActivity;
import com.p1519536gmail.recipies.R;
import com.p1519536gmail.recipies.SingleRecipie;
import com.squareup.picasso.Picasso;
import Model.Category;
import Model.Ingredients;
/**
* A simple {@link Fragment} subclass.
*/
public class recipie extends Fragment {
private ImageView mImageView;
private TextView mTextView;
private View mMainView;
private DatabaseReference mDatabase,mRecipieDatabase;
private RecyclerView Ingredients_list;
public recipie() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mMainView = inflater.inflate(R.layout.fragment_recipie, container, false);
mImageView = (ImageView) mMainView.findViewById(R.id.recipie_image);
mTextView = (TextView)mMainView.findViewById(R.id.recipie_name);
final String user_id = getActivity().getIntent().getStringExtra("Category_id");
mRecipieDatabase = FirebaseDatabase.getInstance().getReference().child("Category").child(user_id);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Food");
//Recycler View
Ingredients_list = mMainView.findViewById(R.id.ingredients_list);
Ingredients_list.setHasFixedSize(true);
Ingredients_list.setLayoutManager(new LinearLayoutManager(getActivity()));
// Inflate the layout for this fragment
return mMainView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final Context c = getActivity().getApplicationContext();
//View Name & Image
mRecipieDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("Name").getValue().toString();
String image = dataSnapshot.child("Image").getValue().toString();
mTextView.setText(name);
Picasso.with(c).load(image).into(mImageView);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Ingredients View Holder
FirebaseRecyclerAdapter<Ingredients, IngredientsViewHolder>firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Ingredients, IngredientsViewHolder>(
Ingredients.class,
R.layout.list_single_ingredients,
IngredientsViewHolder.class,
mDatabase
) {
@Override
protected void populateViewHolder(IngredientsViewHolder viewHolder, Ingredients model, int position) {
viewHolder.setX(model.getX());
}
};
Ingredients_list.setAdapter(firebaseRecyclerAdapter);
}
//ViewHolder
public static class IngredientsViewHolder extends RecyclerView.ViewHolder{
View m2View;
public IngredientsViewHolder(View itemView) {
super(itemView);
m2View = itemView;
}
public void setX(String x){
TextView IngredientsView = (TextView) m2View.findViewById(R.id.txt_ingredients);
IngredientsView.setText(x);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
<TextView
android:id="@+id/txt_ingredients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkbox"
android:layout_alignBottom="@+id/checkbox"
android:layout_marginLeft="10dp"
android:layout_toEndOf="@+id/checkbox"
android:text="Username Placeholder"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
我找到了答案
而不是将Firebase Recycler放入
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}