Search code examples
androidfirebase-realtime-databaseparent-childfirebaseui

Firebase Android accessing child


I request your help today beacause i have a problem with my app using FirebaseDatabase and FireBaseAdapter .

The Structure of the DB look like that:

`{
  "First_Main_Node" : {
    "Child_Node1" : {
      "description" : "Blabla",
      "image" : "Image1.jpg",
      "title" : "My Awesome Title"
    },
    "Child_Node2" : {
      "description" : "Blabla",
      "image" : "Image2.jpg",
      "title" : "My Awesome Title"
    },
    "Child_Node3" : {
      "description" : "Blabla",
      "image" : "Image3.jpg",
      "title" : "My Awesome Title"
    },
    "Child_Node4" : {
      "description" : "Blabla",
      "image" : "Image4.jpg",
      "title" : "My Awesome Title"
    },
    "Child_Node5" : {
      "description" : "Blabla",
      "image" : "Image5.jpg",
      "title" : "My Awesome Title"
    },
    "Child_Node6" : {
      "description" : "Blabla",
      "image" : "Image6.jpg",
      "title" : "My Awesome Title"
    },
    "Child_Node7" : {
      "description" : "Blabla",
      "image" : "Image7.jpg",
      "title" : "My Awesome Title"
    },
    "Child_Node8" : {
      "description" : "Blabla",
      "image" : "Image8.jpg",
      "title" : "My Awesome Title"
    }
  },
  "Second_Main_Node" : {
    "Child_Node1" : {
       "Element" : {
          "image" : "Image1.jpg",
          "price" : "100",
          "title" : "My Awesome Title"
    }
  }
    }`

My App's main activity is a RecyclerView w/ Cardviews that i populate using FirebaseAdapter, it populate the viewHolder by fetching the child of the First_Main_Node using this line of code :

    myRef = FirebaseDatabase.getInstance().getReference().child("/First_Main_Node");

Thing is, as you can see in the DB structure, there is a Second_Main_Node, whom has Nested Child_Node who has Nested Element itself who finally has title,price,image child .

My Problem comes here , i want to access Second_Main_Node > Child_Node1 and Populate another RecyclerView but this Time with all the "Element" children within Child_Node1

I tried the line of code above , by changing child method path parameter by /Second_Main_Node/Child_Node1 , but either the RecyclerView Shows a Cardview with "Null" as title and nothing else happens, or my app crashs immediately after opening the second RecyclerView , i tried to change Reference, Child, both without success, i don't understand how i can access those elements and populate the second RecyclerView , i don't think it's that hard but it been almost a week i'm trying to fix this problem .

Here is the Code Relative to the MainActivity RecyclerView

recyclerView.setHasFixedSize(true);


    recyclerView.setLayoutManager(new GridLayoutManager(this, 2));


    myref = FirebaseDatabase.getInstance().getReference().child("/First_Main_Node");

    FirebaseRecyclerAdapter<Watch, MainActivity.WatchViewHolder> recyclerAdapter = new FirebaseRecyclerAdapter<Watch, MainActivity.WatchViewHolder>(

            Watch.class,
            R.layout.individual_row,
            MainActivity.WatchViewHolder.class,
            myref
    ) {
        @Override
        protected void populateViewHolder(final MainActivity.WatchViewHolder viewHolder, final Watch model, int position) {

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(MainActivity.this, WatchDisplayActivity.class);
                    startActivity(i);

                }
            });


            viewHolder.setTitle(model.getTitle());
            viewHolder.setDescription(model.getDescription());
            viewHolder.setImage(model.getImage());


        }
    };
    recyclerView.setAdapter(recyclerAdapter);


}

public static class WatchViewHolder extends RecyclerView.ViewHolder {

    View mView;
    TextView textView_title;
    TextView textView_description;
    ImageView imageView;


    public WatchViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        textView_title = itemView.findViewById(R.id.tv_titre);
        textView_description = itemView.findViewById(R.id.tv_desc);
        imageView = itemView.findViewById(R.id.iv_image);



    }



    public void setTitle(String title) {
        textView_title.setText(title + "");
    }


    public void setDescription(String description) {


        textView_description.setText(description);
    }

    public void setImage(String image) {
        Picasso.with(mView.getContext())
                .load(image)
                .into(imageView);
    }

}

}

And The One Relative To The Second RecylerView

 rv_display.setHasFixedSize(true);
    rv_display.setLayoutManager(new GridLayoutManager(this,2));


    myref = FirebaseDatabase.getInstance().getReference().child("/Second_Main_Node/Child_Node1");



    FirebaseRecyclerAdapter<Watch, WatchDisplayActivity.WatchViewHolder> recyclerAdapter = new FirebaseRecyclerAdapter<Watch, WatchViewHolder>(

            Watch.class,
            R.layout.individual_row_display,
            WatchDisplayActivity.WatchViewHolder.class,
            myref
    ) {
        @Override
        protected void populateViewHolder(WatchViewHolder viewHolder, Watch model, int position) {

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(),"Test",Toast.LENGTH_SHORT).show();
                }
            });

            viewHolder.setTextView_price(model.getPrice());
            viewHolder.setImageView(model.getImage());
            viewHolder.setTextView_title(model.getTitle());
        }
    };rv_display.setAdapter(recyclerAdapter);


}


public static class WatchViewHolder extends RecyclerView.ViewHolder{

    View mView;
    TextView textView_title;
    TextView textView_price;
    ImageView imageView;


    public WatchViewHolder(View itemView){
        super(itemView);
        mView = itemView;
        textView_price = itemView.findViewById(R.id.tv_price_display);
        textView_title = itemView.findViewById(R.id.tv_titre_display);
        imageView = itemView.findViewById(R.id.iv_image_display);

    }




    public void setTextView_title(String title) {
        textView_title.setText(title + "");
    }

    public void setTextView_price(String price) {
        textView_price.setText(price);
    }

    public void setImageView(String image) {
        Picasso.with(mView.getContext())
                .load(image)
                .into(imageView);
    }
}

And This is the Logcat

02-26 16:57:41.103 19143-19143/package_name E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: fr.package_name, PID: 19143
                                                                 com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Double to String
                                                                     at com.google.android.gms.internal.zg.zzb(Unknown Source)
                                                                     at com.google.android.gms.internal.zg.zza(Unknown Source)
                                                                     at com.google.android.gms.internal.zg.zzb(Unknown Source)
                                                                     at com.google.android.gms.internal.zh.zze(Unknown Source)
                                                                     at com.google.android.gms.internal.zg.zzb(Unknown Source)
                                                                     at com.google.android.gms.internal.zg.zza(Unknown Source)
                                                                     at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                     at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:154)
                                                                     at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:143)
                                                                     at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:183)
                                                                     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.GridLayoutManager.layoutChunk(GridLayoutManager.java:556)
                                                                     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.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
                                                                     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: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:234)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5526)
                                                                     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)
                                                                     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)

Solution

  • Problem solved, indeed price child expected a String and I was giving a long/float type. By adding double quotes on price value like this price: "99,9" instead of price: 99,9

    It solved the problem and I can now access child nodes without crash or exception