我正在开发一个食谱书应用程序。 我的数据结构如下:
Reference
- users
-53
-movies
-id1
(-name
-rating
-ingredients(list)
-recipe
-photo)
-id2 (....)
数据添加正确,但是当我使用以下代码进行检索时,应用程序停止。我的模型类是食谱。
我需要创建一个网格结构来显示所有菜肴。我认为这段代码应该达到这个目的,但事实并非如此。我做错了什么?
当用户单击特定菜肴时,应显示该菜肴的食谱和其他详细信息。我如何在 firebase 中进行这种选择性检索?
package foodelicious.com.foodfinal; public class MainActivity extends AppCompatActivity { private FloatingActionButton fab; ScaleAnimation shrinkAnim; private RecyclerView mRecyclerView; private StaggeredGridLayoutManager mLayoutManager; private TextView tvNoRecipes; private ImageView DPoster; private TextView DMovieName,DIngredients,DRecipeTitle,DRecipe; private RatingBar Drate; //Getting reference to Firebase Database FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference mDatabaseReference = database.getReference(); private static final String userId = "53"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initializing our Recyclerview mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); tvNoRecipes = (TextView) findViewById(R.id.tv_no_recipes); DPoster=(ImageView) findViewById(R.id.Dposter); DIngredients=(TextView) findViewById(R.id.Ding); //scale animation to shrink floating actionbar shrinkAnim = new ScaleAnimation(1.15f, 0f, 1.15f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); if (mRecyclerView != null) { //to enable optimization of recyclerview mRecyclerView.setHasFixedSize(true); } //using staggered grid pattern in recyclerview mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(mLayoutManager); //Say Hello to our new FirebaseUI android Element, i.e., FirebaseRecyclerAdapter FirebaseRecyclerAdapter<Recipe,RecipeViewHolder> adapter = new FirebaseRecyclerAdapter<Recipe, RecipeViewHolder>( Recipe.class, R.layout.recipe_board_item, RecipeViewHolder.class, //referencing the node where we want the database to store the data from our Object mDatabaseReference.child("users").child(userId).child("movies").getRef() ) { @Override protected void populateViewHolder(final RecipeViewHolder viewHolder, Recipe model, int position) { if(tvNoRecipes.getVisibility()== View.VISIBLE){ tvNoRecipes.setVisibility(View.GONE); } viewHolder.tvRecipeName.setText(model.getRecipeName()); viewHolder.ratingBar.setRating(model.getRecipeRating()); Picasso.with(MainActivity.this).load(model.getRecipePoster()).into(viewHolder.ivRecipePoster); viewHolder.ivRecipePoster.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { setContentView(R.layout.recipe_details); } }); } }; mRecyclerView.setAdapter(adapter); fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { getSupportFragmentManager().beginTransaction() .replace(R.id.frame_container, new AddRecipeFragment()) .addToBackStack(null) .commit(); //animation being used to make floating actionbar disappear shrinkAnim.setDuration(400); fab.setAnimation(shrinkAnim); shrinkAnim.start(); shrinkAnim.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { //changing floating actionbar visibility to gone on animation end fab.setVisibility(View.GONE); } @Override public void onAnimationRepeat(Animation animation) { } }); } }); } @Override public void onBackPressed() { super.onBackPressed(); if (fab.getVisibility() == View.GONE) fab.setVisibility(View.VISIBLE); } //ViewHolder for our Firebase UI public static class RecipeViewHolder extends RecyclerView.ViewHolder{ TextView tvRecipeName; RatingBar ratingBar; ImageView ivRecipePoster; public RecipeViewHolder(View v) { super(v); tvRecipeName = (TextView) v.findViewById(R.id.iv_dish_name); ratingBar = (RatingBar) v.findViewById(R.id.rating_bar); ivRecipePoster = (ImageView) v.findViewById(R.id.iv_dish_poster); } } }
LOGCAT
10-05 16:43:08.462 30349-30479/foodelicious.com.foodfinal D/FA: Connected to remote service 10-05 16:43:08.463 30349-30479/foodelicious.com.foodfinal V/FA: Processing queued up service tasks: 1 10-05 16:43:13.592 30349-30479/foodelicious.com.foodfinal V/FA: Inactivity, disconnecting from the service 10-05 16:43:21.549 30349-30354/foodelicious.com.foodfinal I/art: Do partial code cache collection, code=29KB, data=29KB 10-05 16:43:21.550 30349-30354/foodelicious.com.foodfinal I/art: After code cache collection, code=29KB, data=29KB 10-05 16:43:21.550 30349-30354/foodelicious.com.foodfinal I/art: Increasing code cache capacity to 128KB 10-05 16:43:21.617 30349-30349/foodelicious.com.foodfinal D/AndroidRuntime: Shutting down VM 10-05 16:43:21.625 30349-30349/foodelicious.com.foodfinal E/AndroidRuntime: FATAL EXCEPTION: main Process: foodelicious.com.foodfinal, PID: 30349 com.google.firebase.database.DatabaseException: Class foodelicious.com.foodfinal.models.Recipe is missing a constructor with no arguments at com.google.android.gms.internal.zzbqi$zza.zze(Unknown Source) at com.google.android.gms.internal.zzbqi$zza.zzaG(Unknown Source) at com.google.android.gms.internal.zzbqi.zze(Unknown Source) at com.google.android.gms.internal.zzbqi.zzb(Unknown Source) at com.google.android.gms.internal.zzbqi.zza(Unknown Source) at com.google.firebase.database.DataSnapshot.getValue(Unknown Source) at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:147) at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:136) at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:176) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6476) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6509) at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5452) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5718) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5557) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5553) at android.support.v7.widget.LayoutState.next(LayoutState.java:100) at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1570) at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:682) at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:604) 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:871) at android.view.Choreographer.doCallbacks(Choreographer.java:683) at android.view.Choreographer.doFrame(Choreographer.java:616) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6123) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
最佳答案
错误:
com.google.firebase.database.DatabaseException: Class foodelicious.com.foodfinal.models.Recipe is missing a constructor with no arguments at
解决方案:
将默认(无参数)构造函数添加到您的类Recipe
public class Recipe ... {
public Recipe() {
//
}
}
关于java - 当我尝试将数据从 firebase 数据库添加到 firebaseViewHolder 时,Android 应用程序不断停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46582493/