How to add and view child objects in Firebase (Android)?

I am having a similar issue in which I have my files as follow in which I want to be able to view all child items and add new child items.

I understand that you will have to modify the code in the Java file, but I am having trouble understanding how to manipulate my own code and I tried to follow the confusing documentation provided by Firebase.

Below is the structure of my database followed by my code. Any help would be greatly appreciated.

The only problem I have now is how to properly populate the listview with the values of the following structure of my database.

It is as follows: Messages --> Message --> value of which has concatenated string

I hope this isn't confusing

Here is my MainActivity.java

package com.example.sean.messengerappsean; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import com.firebase.client.DataSnapshot; import com.firebase.client.Firebase; import com.firebase.client.FirebaseError; import com.firebase.client.Query; import com.firebase.client.ValueEventListener; import com.firebase.client.collection.ArraySortedMap; import com.firebase.client.realtime.util.StringListReader; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { //TextView mTextFieldCondition; EditText editUsername, editValue; ArrayAdapter arrayAdapter; Button btnSend; Firebase mRef; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onStart() { super.onStart(); //mTextFieldCondition = (TextView) findViewById(R.id.textViewCondition); editUsername = (EditText) findViewById(R.id.editUsername); editValue = (EditText) findViewById(R.id.editValue); btnSend = (Button) findViewById(R.id.btnSend); mRef = new Firebase("https://androidmessagetest.firebaseio.com/"); //CAN USE THE CODE HERE TO PUBLISH TO SPECIFIC LOCATION OR DB //TRYING TO SET LISTVIEW final ListView listView = (ListView) findViewById(R.id.listview); final ArrayList<String> msgsArray = new ArrayList<String>(); //USE THIS FOR POPULATING THE VIEW final ValueEventListener valueEventListener = mRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { //----------CONDITION FOR LISTING DATA---------------- //MIGHT NEED TO PUT VALUES INTO ARRAY AND READ THEM OUT IN ORDER OF PUBLISH //String text = dataSnapshot.child("Message").child("Enter Username3").getValue(String.class); //mTextFieldCondition.setText(text); //POPULATE LISTVIEW for (DataSnapshot Datasnapshot : dataSnapshot.getChildren()) { msgsArray.add(Datasnapshot.child("Messages").child("Message").getKey()); } ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, msgsArray); listView.setAdapter(arrayAdapter); } @Override public void onCancelled(FirebaseError firebaseError) { } }); btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //NEED TO DYNAMICALLY CHANGE VALUE TO USERNAME: MESSAGE String textoutput = String.valueOf(editUsername.getText()); String MessageText = String.valueOf(editValue.getText()); //FIXED MESSAGE ADD CHILD AND HOW TO ADD VALUE TO MESSAGE //TRY TO USE BELOW CODE FOR MESSAGES LATER ON mRef.child("Messages").child("Message").push().setValue(textoutput + ": " + MessageText); } }); //ADD FUNCTION TO LOOK FOR DATA } }

Here is my XML file activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.sean.messengerappsean.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <!--<TextView android:id="@+id/textViewCondition" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/mTextFieldCondition" android:layout_centerHorizontal="true" android:layout_marginBottom="89dp" android:text="Condition" />--> //ENTER IN CODE BELOW FOR VIEWING OUTPUT OF DATA <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" ></ListView> </LinearLayout> <Button android:id="@+id/btnSend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:text="Send" /> <EditText android:id="@+id/editValue" android:layout_width="fill_parent" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignTop="@+id/btnSend" android:layout_toLeftOf="@+id/btnSend" android:layout_toStartOf="@+id/btnSend" android:backgroundTint="@color/colorPrimaryDark" android:inputType="textMultiLine" android:text="Enter Message Here" /> <EditText android:id="@+id/editUsername" android:layout_width="fill_parent" android:layout_height="50dp" android:layout_above="@+id/btnSend" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_gravity="bottom" android:background="@color/colorAccent" android:inputType="textCapWords" android:text="Enter Username" /> </RelativeLayout>


These imports are for classes in the legacy SDK:

import com.firebase.client.DataSnapshot; import com.firebase.client.Firebase; import com.firebase.client.FirebaseError; import com.firebase.client.Query; import com.firebase.client.ValueEventListener;

These are for the new SDK:

import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase;

You cannot mix the two SDKs. You should use the new SDK only. Remove this statement from your build.gradle dependencies:

compile 'com.firebase:firebase-client-android:2.x.x'

and make the approriate code changes to use the new SDK. Documentation and examples are here. There is also an Upgrade Guide.


  • Using UI Editor at runtime on a button click
  • Force application to show TaskBar Icon during OnCreate procedure
  • Telerik Radgrid GridDataItem.DataItem is empty when updating (OnUpdateCommand handler)
  • Call task's updateProgress
  • Drag and Drop in JList is not working
  • How to work with Master Page that is attached to the page via the page's basepage?
  • Symfony2 and MVC - Is extend controller a good practice?
  • Is it safe to accept URL parameters for populating the `url_for` method?
  • ASP.NET RegularExpressionValidator, validate on a non-match?
  • Replace Fragment with another on back button
  • Adding Parent and Child Nodes in TreeView from Sql Server 2008
  • How to upload specific List image using click on Upload button
  • Blackberry 6: how to detect a long click on track pad?
  • unable to get jsonEncode in magento2
  • Authentication failed with Azure Active Directory in Windows Phone
  • How to get listview position?
  • Autofac with Web API 2 - Parameter-less constructor error
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Bypass multiple inheritance in Java
  • how to avoid repetitive constructor in children
  • Highlight and Bold text in JTextPane
  • Recording logins for password protected directories
  • how to do an event when i swipe from fragment to the other
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Web-crawler for facebook in python
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • How do I configure my settings file to work with unit tests?
  • Java static initializers and reflection
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How do I use LINQ to get all the Items that have a particular SubItem?