Firebase RealtimeDatabase Task
Firebase RealtimeDatabase Task
In this lab tutorial, we will implement CRUD (Create, Read, Update, Delete) system in
Android applications using Firebase Realtime Database.
Then go to “Build”> Choose “Realtime Database” > “Get Started” > Create Database.
Go to ‘Rules’ and change as the following.
Now with the assistant window, go to Realtime Database and Click “Get Started with Realtime
Database”.
Step-3:
Click “Connect your app to Firebase”, a pop up window will be opened in your browser.
Choose the app in your firebase control that you have created in Step-1.
Click “Add the Firebase Realtime Database SDK to your app”.
Once both of the process are marked done, you can check in the Gradle files to see if the
dependencies are added properly. If not, you have to do it manually.
Step-4: For adding data to Firebase you have to give permissions for accessing the internet. For
adding these permissions navigate to the app > AndroidManifest.xml and inside that file add the
below permissions to it.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Step-5: Go to the activity_main.xml file and refer to the provided code activity_main.xml. Make
necessary changes according to your project. You can also design as per your requirements.
Step-6: Add the action bar to your app. Go to ‘AndroidManifest.xml’ and replace the theme
portion to this line:
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
go to each activity classes and inside ‘onCreate’ function add the following line:
Step-7: For sending multiple data to the Firebase Realtime database you have to create an
Object class and send that whole object class to Firebase. For creating an object class navigate
to the app > java > your app’s package name > Right-click on it and Click on New > Java
Class > Give a name to your class. In this case, it is EmployeeInfo. Now, define private
attributes employeeName, employeeContactNumber, employeeAddress, employeeID
according to your layout textfields. Then Right Click on your mouse > Select ‘Generate >
Constructor. (Create an empty constructor required when using Firebase Realtime Database.)
Then Right Click again on your mouse > Select ‘Generate > Getter and Setter. (Implement getter
and setter for all the attributes.)
Now inside the onCreate function: Connect the IDs. Create instances for FirebaseDatabase,
DatabaseReference and EmployeeInfo class.
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("EmployeeInfo");
//EmployeeInfo will be the name of the firebase database root node. Each
entries will be added to this node once inserted.
employeeInfo = new EmployeeInfo();
Next you have to implement the button click listener for sendDatabtn. Inside the ‘onClick’
method:
// getting text from our edittext fields.
String name = employeeNameEdt.getText().toString();
String phone = employeePhoneEdt.getText().toString();
String address = employeeAddressEdt.getText().toString();
employeeInfo.setEmployeeName(name);
employeeInfo.setEmployeeContactNumber(phone);
employeeInfo.setEmployeeAddress(address);
Now push the object to your databaseReference object, clear the editText fields and show Toast
message.
databaseReference.push().setValue(employeeInfo)
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Data added",
Toast.LENGTH_SHORT).show();
employeeNameEdt.setText("");
employeePhoneEdt.setText("");
employeeAddressEdt.setText("");
} else {
Toast.makeText(MainActivity.this, "Failed to add data: " +
task.getException(), Toast.LENGTH_SHORT).show();
}
});
You can now run the app and try inserting some data. You can check if the data is properly
inserted in your Firebase project also.
onBindViewHolder populates the ViewHolder with data for the current item in the list.
holder.employeeContact.setText(employee.getEmployeeContactNumber());
holder.employeeAddress.setText(employee.getEmployeeAddress());
}
Step-4: Now you need to fetch data from Firebase Realtime Database. In your Read activity,
retrieve the data from Firebase and populate it into the RecyclerView. Create objects for
RecyclerView, EmployeeAdapter, List.
private RecyclerView recyclerView;
private EmployeeAdapter employeeAdapter;
private List<EmployeeInfo> employeeList;
DatabaseReference databaseReference;
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
The code clears the employeeList to remove any existing data. Then loops through the
DataSnapshot (which contains data from a database, like Firebase). For each child in the
snapshot, it converts the data into an EmployeeInfo object and adds it to the list. After updating
the list, it calls notifyDataSetChanged() on the employeeAdapter, which refreshes the
RecyclerView to display the updated data.
Inside onCancelled function: You can implement a toast.
Finally, the data stored in Firebase can be visualized once you run the app.
Thank you. 😊