'Data is not getting added to Firebase Realtime Database [duplicate]
I've been following this Tutorial for the Realtime Database, this tutorial was last Updated 31 Dec 2020, from GeeksForGeeks-how-to-save-data-to-the-firebase-realtime-database-in-android but for me the Data is not getting Added in the Database
I've used the Firebase Assistant in Android Studio
This is the MainActivity, It has only 2 buttons which go to the activities for adding Data and Viewing them
Java File for MainActivity:
package com.anurag.newemsapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btnAdd, btnView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = findViewById(R.id.btnAdd);
btnView = findViewById(R.id.btnView);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent a = new Intent(MainActivity.this, AddActivity.class);
startActivity(a);
}
});
// the View button is disabled for time-being
}
}
The Add Activity, This is where, I take user input and try to save the data in Firebase
Java File for AddActivity
package com.anurag.newemsapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class AddActivity extends AppCompatActivity {
private EditText etAddEid, etAddEname;
private Button btnAddSave;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
Employee employee;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
etAddEid = findViewById(R.id.etAddEid);
etAddEname = findViewById(R.id.etAddEname);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("Employee");
employee = new Employee();
btnAddSave = findViewById(R.id.btnAddSave);
btnAddSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = etAddEid.getText().toString();
String name = etAddEname.getText().toString();
if (id.length() == 0 && name.length() == 0) {
Toast.makeText(AddActivity.this, "Please add some data.", Toast.LENGTH_SHORT).show();
} else {
addDataToFirebase(id, name);
}
}
});
}
private void addDataToFirebase(String id, String name) {
employee.setEmployeeId(id);
employee.setEmployeeName(name);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
databaseReference.push().setValue(employee);
Toast.makeText(AddActivity.this, "data added", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(AddActivity.this, "Fail to add data " + error, Toast.LENGTH_SHORT).show();
}
});
}
}
Now When I press the Save Button, it does the validation part but, addDataToFirebase doesn't seem to work. I doesn't throw an error, the app runs perfectly fine. it's just the data that is not being added to Firebase Realtime Database
I have added the google-services.json file in the project level, after manually downloading it from the firebase console
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|