I am making an app based on firebase Realtime database.
I want my database structure like the tree given below:-
I am trying to get that data written in my firebase Realtime database by the following java code:-
public class AddActivity extends AppCompatActivity {
private EditText etName;
EditText etRoll;
Button btnAdmit;
Button btnView;
String stName;
String stRoll;
FirebaseDatabase myfire;
DatabaseReference myRef;
@Override
public void onBackPressed() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
etName = (EditText) findViewById(R.id.idUser);
etRoll = (EditText) findViewById(R.id.idPass);
btnView = (Button) findViewById(R.id.idView);
btnAdmit= (Button) findViewById(R.id.idAdmit);
myfire = FirebaseDatabase.getInstance();
myRef = myfire.getReference("Users")
.child("uid");// I dont know what
// to write in this child
// in stead of
// "uid"
btnAdmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stName = etName.getText().toString();
stRoll = etRoll.getText().toString();
etName.setText("");
etRoll.setText("");
myRef.child("101").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
model model =new basic();
model.setFb01name(stName);
model.setFb04roll(stRoll);
myRef.setValue(model);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast,
(ViewGroup) findViewById(R.id.toast_id));
TextView text = (TextView) layout.findViewById(R.id.idToast);
text.setText("Added.");
Toast toast = new Toast(AddActivity.this);
toast.setGravity(Gravity.CENTER ,0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
// btnAdmit.setEnabled(false);
// btnAdmit.setVisibility(View.GONE);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast,
(ViewGroup) findViewById(R.id.toast_id));
TextView text = (TextView) layout.findViewById(R.id.idToast);
text.setText(" Database Error.");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER ,0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
DatabaseError e = null;
Log.e("Database", "Error", e.toException());
}
});
}
});
My model class goes like this:
public class model {
String fb01name;
String fb04roll;
public model() {
}
public model(String fb01name, String fb04roll) {
this.fb01name = fb01name;
this.fb04roll = fb04roll;
}
public String getFb01name() {
return fb01name;
}
public void setFb01name(String fb01name) {
this.fb01name = fb01name;
}
public String getFb04roll() {
return fb04roll;
}
public void setFb04roll(String fb04roll) {
this.fb04roll = fb04roll;
}
}
There is nothing in logcat error message. I am getting nothing written in the firebase Realtime database. I am not able to find the error. The app is displaying the toast "added" as given in on data change method. My database rules are like this:
{
// Allow anyone to read data, but only authenticated content owners can
// make changes to their data
"rules": {
"Users": {
"$uid": {
".read": true,
// or ".read": "auth.uid != null" for only authenticated users
".write": "auth.uid == $uid"
}
}
}
}
Please look into the matter and provide your insight and practical solution.
