Insert Data Into The Firebase Database In Android Apps

Introduction

 
Android is one of the most popular operating systems for mobile. Firebase is the most popular backend for Android. Firebase is the emerging technology in mobile Application development. Firebase is more secure and it is a Cloud based platform. It provides the loT of Services to make your Application very efficient in a secure manner. It is easy to track Crash Report and maintain your data in the Cloud. I will show you how to insert the data into Firebase database in Android apps, using an Android Studio.
 
Requirements
  • Android Studio.
  • Firebase account.
  • Little bit XML and Java knowledge.
  • Android emulator (or) an Android mobile.
  • Stable internet connection.
  • Download link (Android Studio)
Steps should be followed are given below
 
Carefully follow my steps to insert the data into the Firebase database in Android apps, using an Android Studio and I have included the source code given below.
 
Step 1
 
Open an Android Studio and start the new project.
 
Step 2
 
Put the Application name and the company domain. If you wish to use C++ to code the project, mark the Include C++ support, followed by clicking Next.
 
Android
 
Step 3
 
Select an Android minimum SDK. Afterward, you need to choose the minimum SDK. It will show an approximate percentage of people, using that SDK, followed by clicking Next.
 
Step 4
 
Choose Empty activity, followed by clicking Next.
 
Step 5
 
Put the activity name and layout name. Android Studio basically takes Java class name is what you provide for the activity name, and click Finish.
 
Android
 
Step 6
 
Connect your Application to your Firebase account.
 
Step 7
 
This app contains the insert data module, so you need to connect to the real-time database. Click Add the real-time database to your app.
 
Android
 
Step 8
 
It will make some changes to your Gradle module. Click Accept changes.
 
Android
 
Step 9
 
Go to activity_main.xml, followed by clicking Text bottom. This XML file contains the designing code for an Android app. In the activity_main.xml, copy and paste the code given below.
 
Activity_main.xml code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/activity_main"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     tools:context=".MainActivity">  
  12.   
  13.   
  14.     <EditText  
  15.         android:id="@+id/editTextName"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:hint="Enter name" />  
  19.   
  20.     <Spinner  
  21.         android:id="@+id/spinnerGenres"  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_below="@id/editTextName"  
  25.         android:entries="@array/genres"></Spinner>  
  26.   
  27.     <Button  
  28.         android:id="@+id/buttonAddArtist"  
  29.         android:layout_width="match_parent"  
  30.         android:layout_height="wrap_content"  
  31.         android:layout_below="@id/spinnerGenres"  
  32.         android:text="Add" />  
  33.   
  34.     <TextView  
  35.         android:id="@+id/textView"  
  36.         android:layout_width="match_parent"  
  37.         android:layout_height="wrap_content"  
  38.         android:layout_below="@id/buttonAddArtist"  
  39.         android:padding="@dimen/activity_horizontal_margin"  
  40.         android:text="Artists"  
  41.         android:textAlignment="center"  
  42.         android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />  
  43.   
  44.     <TextView  
  45.         android:id="@+id/textView1"  
  46.         android:layout_width="match_parent"  
  47.         android:layout_height="wrap_content"  
  48.         android:layout_below="@id/textView"  
  49.         android:text="Tap on an Artist to add and view tracks"  
  50.         android:textAlignment="center" />  
  51.   
  52.     <ListView  
  53.         android:id="@+id/listViewArtists"  
  54.         android:layout_width="match_parent"  
  55.         android:layout_height="wrap_content"  
  56.         android:layout_below="@+id/textView1"></ListView>  
  57.   
  58. </RelativeLayout>   
Android
 
Step 10
 
Create a new activity_artist.xml file (File ⇒ New ⇒Activity⇒Empty_activity).
 
Go to activity_artist.xml subsequently click Text bottom. This XML file contains the designing code for an Android app. In activity_artist.xml, copy and paste the code given below.
 
activity_artist.xml code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/activity_artist"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     tools:context=".ArtistActivity">  
  12.   
  13.     <TextView  
  14.         android:id="@+id/textViewArtist"  
  15.         android:padding="@dimen/activity_horizontal_margin"  
  16.         android:textAlignment="center"  
  17.         android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"  
  18.         android:textStyle="bold"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content" />  
  21.   
  22.     <EditText  
  23.         android:layout_below="@id/textViewArtist"  
  24.         android:id="@+id/editTextName"  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:hint="Enter track name" />  
  28.   
  29.     <LinearLayout  
  30.         android:orientation="horizontal"  
  31.         android:id="@+id/linearLayout"  
  32.         android:layout_width="match_parent"  
  33.         android:layout_height="wrap_content"  
  34.         android:layout_below="@id/editTextName">  
  35.   
  36.         <SeekBar  
  37.             android:layout_weight="1"  
  38.             android:id="@+id/seekBarRating"  
  39.             android:layout_width="match_parent"  
  40.             android:layout_height="wrap_content"  
  41.             android:max="5"></SeekBar>  
  42.   
  43.         <TextView  
  44.             android:text="1"  
  45.             android:id="@+id/textViewRating"  
  46.             android:layout_width="wrap_content"  
  47.             android:layout_height="wrap_content" />  
  48.   
  49.     </LinearLayout>  
  50.   
  51.   
  52.     <Button  
  53.         android:id="@+id/buttonAddTrack"  
  54.         android:layout_width="match_parent"  
  55.         android:layout_height="wrap_content"  
  56.         android:layout_below="@id/linearLayout"  
  57.         android:text="Add" />  
  58.   
  59.     <TextView  
  60.         android:id="@+id/textView"  
  61.         android:layout_width="match_parent"  
  62.         android:layout_height="wrap_content"  
  63.         android:layout_below="@id/buttonAddTrack"  
  64.         android:padding="@dimen/activity_horizontal_margin"  
  65.         android:text="Tracks"  
  66.         android:textAlignment="center"  
  67.         android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />  
  68.   
  69.     <ListView  
  70.         android:id="@+id/listViewTracks"  
  71.         android:layout_width="match_parent"  
  72.         android:layout_height="wrap_content"  
  73.         android:layout_below="@+id/textView"></ListView>  
  74.   
  75. </RelativeLayout>   
Step 11
 
Create new layout_artist_list.xml file (File ⇒ New ⇒Activity⇒Empty_activity). In the layout_artist_list.xml, copy and paste the code given below.
 
layout_artist_list.xml code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <TextView  
  7.         android:text="Atif Aslam"  
  8.         android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"  
  9.         android:id="@+id/textViewName"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content" />  
  12.   
  13.     <TextView  
  14.         android:text="Rock"  
  15.         android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"  
  16.         android:id="@+id/textViewGenre"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content" />  
  19.   
  20.   
  21. </LinearLayout>   
Step 12
 
Create new update_dialogue.xml file (File ⇒ New ⇒Activity⇒Empty_activity). In the update_dialogue.xml, copy and paste the code given below.
 
update_dialogue.xml code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:padding="@dimen/activity_horizontal_margin">  
  7.   
  8.   
  9.     <EditText  
  10.         android:id="@+id/editTextName"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:hint="Enter name" />  
  14.   
  15.     <Spinner  
  16.         android:id="@+id/spinnerGenres"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_below="@id/editTextName"  
  20.         android:entries="@array/genres"></Spinner>  
  21.   
  22.     <LinearLayout  
  23.         android:layout_width="match_parent"  
  24.         android:layout_height="wrap_content"  
  25.         android:orientation="horizontal">  
  26.   
  27.         <Button  
  28.             android:id="@+id/buttonUpdateArtist"  
  29.             android:layout_width="wrap_content"  
  30.             android:layout_height="wrap_content"  
  31.             android:layout_weight="1"  
  32.             android:text="Update" />  
  33.   
  34.         <Button  
  35.             android:id="@+id/buttonDeleteArtist"  
  36.             android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.             android:layout_weight="1"  
  39.             android:text="Delete" />  
  40.   
  41.     </LinearLayout>  
  42.   
  43.   
  44. </LinearLayout>  
Step 13
 
In the MainActivity.java, copy and paste the code given below. Java programming is the backend language for an Android. Do not replace your package name, else an app will not run.
 
MainActivity.java code
  1. package ganeshannt.insertdata;  
  2.   
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.support.v7.app.AlertDialog;  
  6. import android.support.v7.app.AppCompatActivity;  
  7. import android.text.TextUtils;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.widget.AdapterView;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.ListView;  
  14. import android.widget.Spinner;  
  15. import android.widget.Toast;  
  16.   
  17. import com.google.firebase.database.DataSnapshot;  
  18. import com.google.firebase.database.DatabaseError;  
  19. import com.google.firebase.database.DatabaseReference;  
  20. import com.google.firebase.database.FirebaseDatabase;  
  21. import com.google.firebase.database.ValueEventListener;  
  22.   
  23. import java.util.ArrayList;  
  24. import java.util.List;  
  25.   
  26. public class MainActivity extends AppCompatActivity {  
  27.     public static final String ARTIST_NAME = "net.simplifiedcoding.firebasedatabaseexample.artistname";  
  28.     public static final String ARTIST_ID = "net.simplifiedcoding.firebasedatabaseexample.artistid";  
  29.   
  30.     EditText editTextName;  
  31.     Spinner spinnerGenre;  
  32.     Button buttonAddArtist;  
  33.     ListView listViewArtists;  
  34.   
  35.     //a list to store all the artist from firebase database  
  36.     List<Artist> artists;  
  37.   
  38.     //our database reference object  
  39.     DatabaseReference databaseArtists;  
  40.   
  41.     @Override  
  42.     protected void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.activity_main);  
  45.   
  46.         //getting the reference of artists node  
  47.         databaseArtists = FirebaseDatabase.getInstance().getReference("artists");  
  48.   
  49.         //getting views  
  50.         editTextName = (EditText) findViewById(R.id.editTextName);  
  51.         spinnerGenre = (Spinner) findViewById(R.id.spinnerGenres);  
  52.         listViewArtists = (ListView) findViewById(R.id.listViewArtists);  
  53.   
  54.         buttonAddArtist = (Button) findViewById(R.id.buttonAddArtist);  
  55.   
  56.         //list to store artists  
  57.         artists = new ArrayList<>();  
  58.   
  59.   
  60.         //adding an onclicklistener to button  
  61.         buttonAddArtist.setOnClickListener(new View.OnClickListener() {  
  62.             @Override  
  63.             public void onClick(View view) {  
  64.                 //calling the method addArtist()  
  65.                 //the method is defined below  
  66.                 //this method is actually performing the write operation  
  67.                 addArtist();  
  68.             }  
  69.         });  
  70.   
  71.         //attaching listener to listview  
  72.         listViewArtists.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  73.             @Override  
  74.             public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {  
  75.                 //getting the selected artist  
  76.                 Artist artist = artists.get(i);  
  77.   
  78.                 //creating an intent  
  79.                 Intent intent = new Intent(getApplicationContext(), ArtistActivity.class);  
  80.   
  81.                 //putting artist name and id to intent  
  82.                 intent.putExtra(ARTIST_ID, artist.getArtistId());  
  83.                 intent.putExtra(ARTIST_NAME, artist.getArtistName());  
  84.   
  85.                 //starting the activity with intent  
  86.                 startActivity(intent);  
  87.             }  
  88.         });  
  89.   
  90.         listViewArtists.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {  
  91.             @Override  
  92.             public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {  
  93.                 Artist artist = artists.get(i);  
  94.                 showUpdateDeleteDialog(artist.getArtistId(), artist.getArtistName());  
  95.                 return true;  
  96.             }  
  97.         });  
  98.   
  99.   
  100.     }  
  101.   
  102.     private void showUpdateDeleteDialog(final String artistId, String artistName) {  
  103.   
  104.         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);  
  105.         LayoutInflater inflater = getLayoutInflater();  
  106.         final View dialogView = inflater.inflate(R.layout.update_dialog, null);  
  107.         dialogBuilder.setView(dialogView);  
  108.   
  109.         final EditText editTextName = (EditText) dialogView.findViewById(R.id.editTextName);  
  110.         final Spinner spinnerGenre = (Spinner) dialogView.findViewById(R.id.spinnerGenres);  
  111.         final Button buttonUpdate = (Button) dialogView.findViewById(R.id.buttonUpdateArtist);  
  112.         final Button buttonDelete = (Button) dialogView.findViewById(R.id.buttonDeleteArtist);  
  113.   
  114.         dialogBuilder.setTitle(artistName);  
  115.         final AlertDialog b = dialogBuilder.create();  
  116.         b.show();  
  117.   
  118.   
  119.         buttonUpdate.setOnClickListener(new View.OnClickListener() {  
  120.             @Override  
  121.             public void onClick(View view) {  
  122.                 String name = editTextName.getText().toString().trim();  
  123.                 String genre = spinnerGenre.getSelectedItem().toString();  
  124.                 if (!TextUtils.isEmpty(name)) {  
  125.                     updateArtist(artistId, name, genre);  
  126.                     b.dismiss();  
  127.                 }  
  128.             }  
  129.         });  
  130.   
  131.   
  132.         buttonDelete.setOnClickListener(new View.OnClickListener() {  
  133.             @Override  
  134.             public void onClick(View view) {  
  135.   
  136.                 deleteArtist(artistId);  
  137.                 b.dismiss();  
  138.             }  
  139.         });  
  140.     }  
  141.   
  142.     private boolean updateArtist(String id, String name, String genre) {  
  143.         //getting the specified artist reference  
  144.         DatabaseReference dR = FirebaseDatabase.getInstance().getReference("artists").child(id);  
  145.   
  146.         //updating artist  
  147.         Artist artist = new Artist(id, name, genre);  
  148.         dR.setValue(artist);  
  149.         Toast.makeText(getApplicationContext(), "Artist Updated", Toast.LENGTH_LONG).show();  
  150.         return true;  
  151.     }  
  152.   
  153.     private boolean deleteArtist(String id) {  
  154.         //getting the specified artist reference  
  155.         DatabaseReference dR = FirebaseDatabase.getInstance().getReference("artists").child(id);  
  156.   
  157.         //removing artist  
  158.         dR.removeValue();  
  159.   
  160.         //getting the tracks reference for the specified artist  
  161.         DatabaseReference drTracks = FirebaseDatabase.getInstance().getReference("tracks").child(id);  
  162.   
  163.         //removing all tracks  
  164.         drTracks.removeValue();  
  165.         Toast.makeText(getApplicationContext(), "Artist Deleted", Toast.LENGTH_LONG).show();  
  166.   
  167.         return true;  
  168.     }  
  169.   
  170.     @Override  
  171.     protected void onStart() {  
  172.         super.onStart();  
  173.         //attaching value event listener  
  174.         databaseArtists.addValueEventListener(new ValueEventListener() {  
  175.             @Override  
  176.             public void onDataChange(DataSnapshot dataSnapshot) {  
  177.   
  178.                 //clearing the previous artist list  
  179.                 artists.clear();  
  180.   
  181.                 //iterating through all the nodes  
  182.                 for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {  
  183.                     //getting artist  
  184.                     Artist artist = postSnapshot.getValue(Artist.class);  
  185.                     //adding artist to the list  
  186.                     artists.add(artist);  
  187.                 }  
  188.   
  189.                 //creating adapter  
  190.                 ArtistList artistAdapter = new ArtistList(MainActivity.this, artists);  
  191.                 //attaching adapter to the listview  
  192.                 listViewArtists.setAdapter(artistAdapter);  
  193.             }  
  194.   
  195.             @Override  
  196.             public void onCancelled(DatabaseError databaseError) {  
  197.   
  198.             }  
  199.         });  
  200.     }  
  201.   
  202.   
  203.     /* 
  204.     * This method is saving a new artist to the 
  205.     * Firebase Realtime Database 
  206.     * */  
  207.     private void addArtist() {  
  208.         //getting the values to save  
  209.         String name = editTextName.getText().toString().trim();  
  210.         String genre = spinnerGenre.getSelectedItem().toString();  
  211.   
  212.         //checking if the value is provided  
  213.         if (!TextUtils.isEmpty(name)) {  
  214.   
  215.             //getting a unique id using push().getKey() method  
  216.             //it will create a unique id and we will use it as the Primary Key for our Artist  
  217.             String id = databaseArtists.push().getKey();  
  218.   
  219.             //creating an Artist Object  
  220.             Artist artist = new Artist(id, name, genre);  
  221.   
  222.             //Saving the Artist  
  223.             databaseArtists.child(id).setValue(artist);  
  224.   
  225.             //setting edittext to blank again  
  226.             editTextName.setText("");  
  227.   
  228.             //displaying a success toast  
  229.             Toast.makeText(this"Artist added", Toast.LENGTH_LONG).show();  
  230.         } else {  
  231.             //if the value is not given displaying a toast  
  232.             Toast.makeText(this"Please enter a name", Toast.LENGTH_LONG).show();  
  233.         }  
  234.     }  
  235. }  
Step 14 
 
Create new Artist.java file (File ⇒ New ⇒Java class). In the Artist.java, copy and paste the code given below.
 
Artist.java code
  1. package ganeshannt.insertdata;  
  2.   
  3. import com.google.firebase.database.IgnoreExtraProperties;  
  4.   
  5. @IgnoreExtraProperties  
  6. public class Artist {  
  7.     private String artistId;  
  8.     private String artistName;  
  9.     private String artistGenre;  
  10.   
  11.     public Artist(){  
  12.         //this constructor is required  
  13.     }  
  14.   
  15.     public Artist(String artistId, String artistName, String artistGenre) {  
  16.         this.artistId = artistId;  
  17.         this.artistName = artistName;  
  18.         this.artistGenre = artistGenre;  
  19.     }  
  20.   
  21.     public String getArtistId() {  
  22.         return artistId;  
  23.     }  
  24.   
  25.     public String getArtistName() {  
  26.         return artistName;  
  27.     }  
  28.   
  29.     public String getArtistGenre() {  
  30.         return artistGenre;  
  31.     }  
  32. }  
Step 15 
 
Create new ArtistActivity.java file (File ⇒ New ⇒Java class). In the ArtistActivity.java, copy and paste the code given below.
 
ArtistActivity.java code
  1. package ganeshannt.insertdata;  
  2.   
  3. import android.content.Intent;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.text.TextUtils;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.ListView;  
  11. import android.widget.SeekBar;  
  12. import android.widget.TextView;  
  13. import android.widget.Toast;  
  14.   
  15. import com.google.firebase.database.DataSnapshot;  
  16. import com.google.firebase.database.DatabaseError;  
  17. import com.google.firebase.database.DatabaseReference;  
  18. import com.google.firebase.database.FirebaseDatabase;  
  19. import com.google.firebase.database.ValueEventListener;  
  20.   
  21. import java.util.ArrayList;  
  22. import java.util.List;  
  23.   
  24. public class ArtistActivity extends AppCompatActivity {  
  25.   
  26.     Button buttonAddTrack;  
  27.     EditText editTextTrackName;  
  28.     SeekBar seekBarRating;  
  29.     TextView textViewRating, textViewArtist;  
  30.     ListView listViewTracks;  
  31.   
  32.     DatabaseReference databaseTracks;  
  33.   
  34.     List<Track> tracks;  
  35.   
  36.     @Override  
  37.     protected void onCreate(Bundle savedInstanceState) {  
  38.         super.onCreate(savedInstanceState);  
  39.         setContentView(R.layout.activity_artist);  
  40.   
  41.         Intent intent = getIntent();  
  42.   
  43.         /* 
  44.         * this line is important 
  45.         * this time we are not getting the reference of a direct node 
  46.         * but inside the node track we are creating a new child with the artist id 
  47.         * and inside that node we will store all the tracks with unique ids 
  48.         * */  
  49.         databaseTracks = FirebaseDatabase.getInstance().getReference("tracks").child(intent.getStringExtra(MainActivity.ARTIST_ID));  
  50.   
  51.         buttonAddTrack = (Button) findViewById(R.id.buttonAddTrack);  
  52.         editTextTrackName = (EditText) findViewById(R.id.editTextName);  
  53.         seekBarRating = (SeekBar) findViewById(R.id.seekBarRating);  
  54.         textViewRating = (TextView) findViewById(R.id.textViewRating);  
  55.         textViewArtist = (TextView) findViewById(R.id.textViewArtist);  
  56.         listViewTracks = (ListView) findViewById(R.id.listViewTracks);  
  57.   
  58.         tracks = new ArrayList<>();  
  59.   
  60.         textViewArtist.setText(intent.getStringExtra(MainActivity.ARTIST_NAME));  
  61.   
  62.         seekBarRating.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {  
  63.             @Override  
  64.             public void onProgressChanged(SeekBar seekBar, int i, boolean b) {  
  65.                 textViewRating.setText(String.valueOf(i));  
  66.             }  
  67.   
  68.             @Override  
  69.             public void onStartTrackingTouch(SeekBar seekBar) {  
  70.   
  71.             }  
  72.   
  73.             @Override  
  74.             public void onStopTrackingTouch(SeekBar seekBar) {  
  75.   
  76.             }  
  77.         });  
  78.   
  79.         buttonAddTrack.setOnClickListener(new View.OnClickListener() {  
  80.             @Override  
  81.             public void onClick(View view) {  
  82.                 saveTrack();  
  83.             }  
  84.         });  
  85.     }  
  86.   
  87.     @Override  
  88.     protected void onStart() {  
  89.         super.onStart();  
  90.   
  91.         databaseTracks.addValueEventListener(new ValueEventListener() {  
  92.             @Override  
  93.             public void onDataChange(DataSnapshot dataSnapshot) {  
  94.                 tracks.clear();  
  95.                 for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {  
  96.                     Track track = postSnapshot.getValue(Track.class);  
  97.                     tracks.add(track);  
  98.                 }  
  99.                 TrackList trackListAdapter = new TrackList(ArtistActivity.this, tracks);  
  100.                 listViewTracks.setAdapter(trackListAdapter);  
  101.             }  
  102.   
  103.             @Override  
  104.             public void onCancelled(DatabaseError databaseError) {  
  105.   
  106.             }  
  107.         });  
  108.     }  
  109.   
  110.     private void saveTrack() {  
  111.         String trackName = editTextTrackName.getText().toString().trim();  
  112.         int rating = seekBarRating.getProgress();  
  113.         if (!TextUtils.isEmpty(trackName)) {  
  114.             String id  = databaseTracks.push().getKey();  
  115.             Track track = new Track(id, trackName, rating);  
  116.             databaseTracks.child(id).setValue(track);  
  117.             Toast.makeText(this"Track saved", Toast.LENGTH_LONG).show();  
  118.             editTextTrackName.setText("");  
  119.         } else {  
  120.             Toast.makeText(this"Please enter track name", Toast.LENGTH_LONG).show();  
  121.         }  
  122.     }  
  123. }  
Step 16 
 
Create a new ArtistList.java file (File ⇒ New ⇒Java class). In the ArtistList.java, copy and paste the code given below.
 
ArtistList.java code
  1. package ganeshannt.insertdata;  
  2.   
  3. import android.app.Activity;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.TextView;  
  9.   
  10. import java.util.List;  
  11.   
  12. public class ArtistList extends ArrayAdapter<Artist> {  
  13.     private Activity context;  
  14.     List<Artist> artists;  
  15.   
  16.     public ArtistList(Activity context, List<Artist> artists) {  
  17.         super(context, R.layout.layout_artist_list, artists);  
  18.         this.context = context;  
  19.         this.artists = artists;  
  20.     }  
  21.   
  22.   
  23.     @Override  
  24.     public View getView(int position, View convertView, ViewGroup parent) {  
  25.         LayoutInflater inflater = context.getLayoutInflater();  
  26.         View listViewItem = inflater.inflate(R.layout.layout_artist_list, nulltrue);  
  27.   
  28.         TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);  
  29.         TextView textViewGenre = (TextView) listViewItem.findViewById(R.id.textViewGenre);  
  30.   
  31.         Artist artist = artists.get(position);  
  32.         textViewName.setText(artist.getArtistName());  
  33.         textViewGenre.setText(artist.getArtistGenre());  
  34.   
  35.         return listViewItem;  
  36.     }  
  37. }  
Step 17 
 
Create new Track.java file (File ⇒ New ⇒Java class). In Track.java, copy and paste the code given below.
 
Track.java code
  1. package ganeshannt.insertdata;  
  2.   
  3. import com.google.firebase.database.IgnoreExtraProperties;  
  4.   
  5. @IgnoreExtraProperties  
  6. public class Track {  
  7.     private String id;  
  8.     private String trackName;  
  9.     private int rating;  
  10.   
  11.     public Track() {  
  12.   
  13.     }  
  14.   
  15.     public Track(String id, String trackName, int rating) {  
  16.         this.trackName = trackName;  
  17.         this.rating = rating;  
  18.         this.id = id;  
  19.     }  
  20.   
  21.     public String getTrackName() {  
  22.         return trackName;  
  23.     }  
  24.   
  25.     public int getRating() {  
  26.         return rating;  
  27.     }  
  28. }  
Step 18 
 
Create a new TrackList.java file (File ⇒ New ⇒Java class). In the TrackList.java, copy and paste the code given below.
 
TrackList.java code
  1. package ganeshannt.insertdata;  
  2.   
  3. import android.app.Activity;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.TextView;  
  9.   
  10. import java.util.List;  
  11.   
  12. public class TrackList extends ArrayAdapter<Track> {  
  13.     private Activity context;  
  14.     List<Track> tracks;  
  15.   
  16.     public TrackList(Activity context, List<Track> tracks) {  
  17.         super(context, R.layout.layout_artist_list, tracks);  
  18.         this.context = context;  
  19.         this.tracks = tracks;  
  20.     }  
  21.   
  22.   
  23.     @Override  
  24.     public View getView(int position, View convertView, ViewGroup parent) {  
  25.         LayoutInflater inflater = context.getLayoutInflater();  
  26.         View listViewItem = inflater.inflate(R.layout.layout_artist_list, nulltrue);  
  27.   
  28.         TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);  
  29.         TextView textViewRating = (TextView) listViewItem.findViewById(R.id.textViewGenre);  
  30.   
  31.         Track track = tracks.get(position);  
  32.         textViewName.setText(track.getTrackName());  
  33.         textViewRating.setText(String.valueOf(track.getRating()));  
  34.   
  35.         return listViewItem;  
  36.     }  
  37. }   
Step 19
 
Run the Application, followed by choosing Virtual Machine. Click OK.
 
Deliverables
 
Here, we need to successfully insert the data into Firebase in an Android app, using an Android Studio, which is created and executed.
 
Android
 
Android
 
Android
 
Android
 
Now, check the Firebase database.
 
Android
 
The data was added successfully in the Firebase database.


Similar Articles