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
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. <EditText
  13. android:id="@+id/editTextName"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:hint="Enter name" />
  17. <Spinner
  18. android:id="@+id/spinnerGenres"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_below="@id/editTextName"
  22. android:entries="@array/genres"></Spinner>
  23. <Button
  24. android:id="@+id/buttonAddArtist"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:layout_below="@id/spinnerGenres"
  28. android:text="Add" />
  29. <TextView
  30. android:id="@+id/textView"
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:layout_below="@id/buttonAddArtist"
  34. android:padding="@dimen/activity_horizontal_margin"
  35. android:text="Artists"
  36. android:textAlignment="center"
  37. android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
  38. <TextView
  39. android:id="@+id/textView1"
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content"
  42. android:layout_below="@id/textView"
  43. android:text="Tap on an Artist to add and view tracks"
  44. android:textAlignment="center" />
  45. <ListView
  46. android:id="@+id/listViewArtists"
  47. android:layout_width="match_parent"
  48. android:layout_height="wrap_content"
  49. android:layout_below="@+id/textView1"></ListView>
  50. </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. <TextView
  13. android:id="@+id/textViewArtist"
  14. android:padding="@dimen/activity_horizontal_margin"
  15. android:textAlignment="center"
  16. android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
  17. android:textStyle="bold"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content" />
  20. <EditText
  21. android:layout_below="@id/textViewArtist"
  22. android:id="@+id/editTextName"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:hint="Enter track name" />
  26. <LinearLayout
  27. android:orientation="horizontal"
  28. android:id="@+id/linearLayout"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:layout_below="@id/editTextName">
  32. <SeekBar
  33. android:layout_weight="1"
  34. android:id="@+id/seekBarRating"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:max="5"></SeekBar>
  38. <TextView
  39. android:text="1"
  40. android:id="@+id/textViewRating"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content" />
  43. </LinearLayout>
  44. <Button
  45. android:id="@+id/buttonAddTrack"
  46. android:layout_width="match_parent"
  47. android:layout_height="wrap_content"
  48. android:layout_below="@id/linearLayout"
  49. android:text="Add" />
  50. <TextView
  51. android:id="@+id/textView"
  52. android:layout_width="match_parent"
  53. android:layout_height="wrap_content"
  54. android:layout_below="@id/buttonAddTrack"
  55. android:padding="@dimen/activity_horizontal_margin"
  56. android:text="Tracks"
  57. android:textAlignment="center"
  58. android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
  59. <ListView
  60. android:id="@+id/listViewTracks"
  61. android:layout_width="match_parent"
  62. android:layout_height="wrap_content"
  63. android:layout_below="@+id/textView"></ListView>
  64. </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. <TextView
  6. android:text="Atif Aslam"
  7. android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
  8. android:id="@+id/textViewName"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content" />
  11. <TextView
  12. android:text="Rock"
  13. android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
  14. android:id="@+id/textViewGenre"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content" />
  17. </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. <EditText
  8. android:id="@+id/editTextName"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:hint="Enter name" />
  12. <Spinner
  13. android:id="@+id/spinnerGenres"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_below="@id/editTextName"
  17. android:entries="@array/genres"></Spinner>
  18. <LinearLayout
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:orientation="horizontal">
  22. <Button
  23. android:id="@+id/buttonUpdateArtist"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_weight="1"
  27. android:text="Update" />
  28. <Button
  29. android:id="@+id/buttonDeleteArtist"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_weight="1"
  33. android:text="Delete" />
  34. </LinearLayout>
  35. </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. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.support.v7.app.AlertDialog;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.text.TextUtils;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.widget.AdapterView;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.ListView;
  13. import android.widget.Spinner;
  14. import android.widget.Toast;
  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. import java.util.ArrayList;
  21. import java.util.List;
  22. public class MainActivity extends AppCompatActivity {
  23. public static final String ARTIST_NAME = "net.simplifiedcoding.firebasedatabaseexample.artistname";
  24. public static final String ARTIST_ID = "net.simplifiedcoding.firebasedatabaseexample.artistid";
  25. EditText editTextName;
  26. Spinner spinnerGenre;
  27. Button buttonAddArtist;
  28. ListView listViewArtists;
  29. //a list to store all the artist from firebase database
  30. List<Artist> artists;
  31. //our database reference object
  32. DatabaseReference databaseArtists;
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.activity_main);
  37. //getting the reference of artists node
  38. databaseArtists = FirebaseDatabase.getInstance().getReference("artists");
  39. //getting views
  40. editTextName = (EditText) findViewById(R.id.editTextName);
  41. spinnerGenre = (Spinner) findViewById(R.id.spinnerGenres);
  42. listViewArtists = (ListView) findViewById(R.id.listViewArtists);
  43. buttonAddArtist = (Button) findViewById(R.id.buttonAddArtist);
  44. //list to store artists
  45. artists = new ArrayList<>();
  46. //adding an onclicklistener to button
  47. buttonAddArtist.setOnClickListener(new View.OnClickListener() {
  48. @Override
  49. public void onClick(View view) {
  50. //calling the method addArtist()
  51. //the method is defined below
  52. //this method is actually performing the write operation
  53. addArtist();
  54. }
  55. });
  56. //attaching listener to listview
  57. listViewArtists.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  58. @Override
  59. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  60. //getting the selected artist
  61. Artist artist = artists.get(i);
  62. //creating an intent
  63. Intent intent = new Intent(getApplicationContext(), ArtistActivity.class);
  64. //putting artist name and id to intent
  65. intent.putExtra(ARTIST_ID, artist.getArtistId());
  66. intent.putExtra(ARTIST_NAME, artist.getArtistName());
  67. //starting the activity with intent
  68. startActivity(intent);
  69. }
  70. });
  71. listViewArtists.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  72. @Override
  73. public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
  74. Artist artist = artists.get(i);
  75. showUpdateDeleteDialog(artist.getArtistId(), artist.getArtistName());
  76. return true;
  77. }
  78. });
  79. }
  80. private void showUpdateDeleteDialog(final String artistId, String artistName) {
  81. AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
  82. LayoutInflater inflater = getLayoutInflater();
  83. final View dialogView = inflater.inflate(R.layout.update_dialog, null);
  84. dialogBuilder.setView(dialogView);
  85. final EditText editTextName = (EditText) dialogView.findViewById(R.id.editTextName);
  86. final Spinner spinnerGenre = (Spinner) dialogView.findViewById(R.id.spinnerGenres);
  87. final Button buttonUpdate = (Button) dialogView.findViewById(R.id.buttonUpdateArtist);
  88. final Button buttonDelete = (Button) dialogView.findViewById(R.id.buttonDeleteArtist);
  89. dialogBuilder.setTitle(artistName);
  90. final AlertDialog b = dialogBuilder.create();
  91. b.show();
  92. buttonUpdate.setOnClickListener(new View.OnClickListener() {
  93. @Override
  94. public void onClick(View view) {
  95. String name = editTextName.getText().toString().trim();
  96. String genre = spinnerGenre.getSelectedItem().toString();
  97. if (!TextUtils.isEmpty(name)) {
  98. updateArtist(artistId, name, genre);
  99. b.dismiss();
  100. }
  101. }
  102. });
  103. buttonDelete.setOnClickListener(new View.OnClickListener() {
  104. @Override
  105. public void onClick(View view) {
  106. deleteArtist(artistId);
  107. b.dismiss();
  108. }
  109. });
  110. }
  111. private boolean updateArtist(String id, String name, String genre) {
  112. //getting the specified artist reference
  113. DatabaseReference dR = FirebaseDatabase.getInstance().getReference("artists").child(id);
  114. //updating artist
  115. Artist artist = new Artist(id, name, genre);
  116. dR.setValue(artist);
  117. Toast.makeText(getApplicationContext(), "Artist Updated", Toast.LENGTH_LONG).show();
  118. return true;
  119. }
  120. private boolean deleteArtist(String id) {
  121. //getting the specified artist reference
  122. DatabaseReference dR = FirebaseDatabase.getInstance().getReference("artists").child(id);
  123. //removing artist
  124. dR.removeValue();
  125. //getting the tracks reference for the specified artist
  126. DatabaseReference drTracks = FirebaseDatabase.getInstance().getReference("tracks").child(id);
  127. //removing all tracks
  128. drTracks.removeValue();
  129. Toast.makeText(getApplicationContext(), "Artist Deleted", Toast.LENGTH_LONG).show();
  130. return true;
  131. }
  132. @Override
  133. protected void onStart() {
  134. super.onStart();
  135. //attaching value event listener
  136. databaseArtists.addValueEventListener(new ValueEventListener() {
  137. @Override
  138. public void onDataChange(DataSnapshot dataSnapshot) {
  139. //clearing the previous artist list
  140. artists.clear();
  141. //iterating through all the nodes
  142. for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
  143. //getting artist
  144. Artist artist = postSnapshot.getValue(Artist.class);
  145. //adding artist to the list
  146. artists.add(artist);
  147. }
  148. //creating adapter
  149. ArtistList artistAdapter = new ArtistList(MainActivity.this, artists);
  150. //attaching adapter to the listview
  151. listViewArtists.setAdapter(artistAdapter);
  152. }
  153. @Override
  154. public void onCancelled(DatabaseError databaseError) {
  155. }
  156. });
  157. }
  158. /*
  159. * This method is saving a new artist to the
  160. * Firebase Realtime Database
  161. * */
  162. private void addArtist() {
  163. //getting the values to save
  164. String name = editTextName.getText().toString().trim();
  165. String genre = spinnerGenre.getSelectedItem().toString();
  166. //checking if the value is provided
  167. if (!TextUtils.isEmpty(name)) {
  168. //getting a unique id using push().getKey() method
  169. //it will create a unique id and we will use it as the Primary Key for our Artist
  170. String id = databaseArtists.push().getKey();
  171. //creating an Artist Object
  172. Artist artist = new Artist(id, name, genre);
  173. //Saving the Artist
  174. databaseArtists.child(id).setValue(artist);
  175. //setting edittext to blank again
  176. editTextName.setText("");
  177. //displaying a success toast
  178. Toast.makeText(this, "Artist added", Toast.LENGTH_LONG).show();
  179. } else {
  180. //if the value is not given displaying a toast
  181. Toast.makeText(this, "Please enter a name", Toast.LENGTH_LONG).show();
  182. }
  183. }
  184. }
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. import com.google.firebase.database.IgnoreExtraProperties;
  3. @IgnoreExtraProperties
  4. public class Artist {
  5. private String artistId;
  6. private String artistName;
  7. private String artistGenre;
  8. public Artist(){
  9. //this constructor is required
  10. }
  11. public Artist(String artistId, String artistName, String artistGenre) {
  12. this.artistId = artistId;
  13. this.artistName = artistName;
  14. this.artistGenre = artistGenre;
  15. }
  16. public String getArtistId() {
  17. return artistId;
  18. }
  19. public String getArtistName() {
  20. return artistName;
  21. }
  22. public String getArtistGenre() {
  23. return artistGenre;
  24. }
  25. }
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. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.text.TextUtils;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.ListView;
  10. import android.widget.SeekBar;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13. import com.google.firebase.database.DataSnapshot;
  14. import com.google.firebase.database.DatabaseError;
  15. import com.google.firebase.database.DatabaseReference;
  16. import com.google.firebase.database.FirebaseDatabase;
  17. import com.google.firebase.database.ValueEventListener;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. public class ArtistActivity extends AppCompatActivity {
  21. Button buttonAddTrack;
  22. EditText editTextTrackName;
  23. SeekBar seekBarRating;
  24. TextView textViewRating, textViewArtist;
  25. ListView listViewTracks;
  26. DatabaseReference databaseTracks;
  27. List<Track> tracks;
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_artist);
  32. Intent intent = getIntent();
  33. /*
  34. * this line is important
  35. * this time we are not getting the reference of a direct node
  36. * but inside the node track we are creating a new child with the artist id
  37. * and inside that node we will store all the tracks with unique ids
  38. * */
  39. databaseTracks = FirebaseDatabase.getInstance().getReference("tracks").child(intent.getStringExtra(MainActivity.ARTIST_ID));
  40. buttonAddTrack = (Button) findViewById(R.id.buttonAddTrack);
  41. editTextTrackName = (EditText) findViewById(R.id.editTextName);
  42. seekBarRating = (SeekBar) findViewById(R.id.seekBarRating);
  43. textViewRating = (TextView) findViewById(R.id.textViewRating);
  44. textViewArtist = (TextView) findViewById(R.id.textViewArtist);
  45. listViewTracks = (ListView) findViewById(R.id.listViewTracks);
  46. tracks = new ArrayList<>();
  47. textViewArtist.setText(intent.getStringExtra(MainActivity.ARTIST_NAME));
  48. seekBarRating.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  49. @Override
  50. public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  51. textViewRating.setText(String.valueOf(i));
  52. }
  53. @Override
  54. public void onStartTrackingTouch(SeekBar seekBar) {
  55. }
  56. @Override
  57. public void onStopTrackingTouch(SeekBar seekBar) {
  58. }
  59. });
  60. buttonAddTrack.setOnClickListener(new View.OnClickListener() {
  61. @Override
  62. public void onClick(View view) {
  63. saveTrack();
  64. }
  65. });
  66. }
  67. @Override
  68. protected void onStart() {
  69. super.onStart();
  70. databaseTracks.addValueEventListener(new ValueEventListener() {
  71. @Override
  72. public void onDataChange(DataSnapshot dataSnapshot) {
  73. tracks.clear();
  74. for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
  75. Track track = postSnapshot.getValue(Track.class);
  76. tracks.add(track);
  77. }
  78. TrackList trackListAdapter = new TrackList(ArtistActivity.this, tracks);
  79. listViewTracks.setAdapter(trackListAdapter);
  80. }
  81. @Override
  82. public void onCancelled(DatabaseError databaseError) {
  83. }
  84. });
  85. }
  86. private void saveTrack() {
  87. String trackName = editTextTrackName.getText().toString().trim();
  88. int rating = seekBarRating.getProgress();
  89. if (!TextUtils.isEmpty(trackName)) {
  90. String id = databaseTracks.push().getKey();
  91. Track track = new Track(id, trackName, rating);
  92. databaseTracks.child(id).setValue(track);
  93. Toast.makeText(this, "Track saved", Toast.LENGTH_LONG).show();
  94. editTextTrackName.setText("");
  95. } else {
  96. Toast.makeText(this, "Please enter track name", Toast.LENGTH_LONG).show();
  97. }
  98. }
  99. }
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. import android.app.Activity;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.TextView;
  8. import java.util.List;
  9. public class ArtistList extends ArrayAdapter<Artist> {
  10. private Activity context;
  11. List<Artist> artists;
  12. public ArtistList(Activity context, List<Artist> artists) {
  13. super(context, R.layout.layout_artist_list, artists);
  14. this.context = context;
  15. this.artists = artists;
  16. }
  17. @Override
  18. public View getView(int position, View convertView, ViewGroup parent) {
  19. LayoutInflater inflater = context.getLayoutInflater();
  20. View listViewItem = inflater.inflate(R.layout.layout_artist_list, null, true);
  21. TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
  22. TextView textViewGenre = (TextView) listViewItem.findViewById(R.id.textViewGenre);
  23. Artist artist = artists.get(position);
  24. textViewName.setText(artist.getArtistName());
  25. textViewGenre.setText(artist.getArtistGenre());
  26. return listViewItem;
  27. }
  28. }
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. import com.google.firebase.database.IgnoreExtraProperties;
  3. @IgnoreExtraProperties
  4. public class Track {
  5. private String id;
  6. private String trackName;
  7. private int rating;
  8. public Track() {
  9. }
  10. public Track(String id, String trackName, int rating) {
  11. this.trackName = trackName;
  12. this.rating = rating;
  13. this.id = id;
  14. }
  15. public String getTrackName() {
  16. return trackName;
  17. }
  18. public int getRating() {
  19. return rating;
  20. }
  21. }
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. import android.app.Activity;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.TextView;
  8. import java.util.List;
  9. public class TrackList extends ArrayAdapter<Track> {
  10. private Activity context;
  11. List<Track> tracks;
  12. public TrackList(Activity context, List<Track> tracks) {
  13. super(context, R.layout.layout_artist_list, tracks);
  14. this.context = context;
  15. this.tracks = tracks;
  16. }
  17. @Override
  18. public View getView(int position, View convertView, ViewGroup parent) {
  19. LayoutInflater inflater = context.getLayoutInflater();
  20. View listViewItem = inflater.inflate(R.layout.layout_artist_list, null, true);
  21. TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
  22. TextView textViewRating = (TextView) listViewItem.findViewById(R.id.textViewGenre);
  23. Track track = tracks.get(position);
  24. textViewName.setText(track.getTrackName());
  25. textViewRating.setText(String.valueOf(track.getRating()));
  26. return listViewItem;
  27. }
  28. }
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.