1.Atoset child class
package abhiandroid.com.practice;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
EditText editText1, editText2;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = findViewById(R.id.editText1);
editText2 = findViewById(R.id.editText2);
button = findViewById(R.id.button);
// Write a message to the database
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
String name = editText1.getText().toString();
String address = editText2.getText().toString();
Model model = new Model(name,address);
myRef.child(name).setValue(model);
// myRef.setValue(name);
Toast.makeText(MainActivity.this,"hi"+ name, Toast.LENGTH_SHORT).show();
}
});
}
}
2. model class because data Set purpose.
package abhiandroid.com.practice;
public class Model {
String name, address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Model(String name, String address) {
this.name = name;
this.address = address;
}
}
____________________________________________________________________________--
3.Direct data fetch
package abhiandroid.com.practice;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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 MainActivity4 extends AppCompatActivity {
//direct data fetch
FirebaseDatabase database;
DatabaseReference myRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
database = FirebaseDatabase.getInstance();
myRef = database.getReference("Student");
// Read from the database
myRef.child("s1").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
student Student = dataSnapshot.getValue(student.class);
if(Student != null) {
String name = Student.name;
Log.d("tag", "Value is:" + name);
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
// Log.w(TAG, "Failed to read value.", error.toException());
}
});
}
}
4.student
package abhiandroid.com.practice;
public class student {
public String name ;
public student(){
}
public student(String name) {
this.name = name;
}
}
5.
6.