Firebase Example Store And Retrieve Information(Data)

This example will show how to connect with google firebase cloud service. For every application, we need the database to store the information and later we can retrieve it and show it to the user. Firebase providing this functionality. For that, first you need to create an account. Below link to create an account on firebase:-

After that, Click on Go to Dashboard at the right corner and in dashboard Click on Create a new project and give a name to your project.

Steps:-

1) Add Gradle to your project;
2) Create a Model file.
3) Crate firebase object to store data.

Add the following dependacies in you Gradle file:-
compile 'com.firebase:firebase-client-android:2.4.0'
compile "com.google.firebase:firebase-database:9.0.0"
compile 'com.google.firebase:firebase-storage:9.0.1'


Create a model class according to your data.

PersonDetails.java
@IgnoreExtraProperties
public class PersonDetails {
String name;
String password;
public PersonDetails(){}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Create a separate config file for your firebase object;
FirebaseConfig.java

public class FirebaseConfig {
public static String config="https://testapplication0.firebaseio.com/"; 
// Put your project URL here
}


Now we are going to add the data in the firebase. Do the following changes in the java file.
Main.java

public class MainActivity extends AppCompatActivity {
 EditText etname, etpassword;
 Button btnSend;

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 Firebase.setAndroidContext(this);
 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);

 etname = (EditText) findViewById(R.id.name);
 etpassword = (EditText) findViewById(R.id.password);
 btnSend = (Button) findViewById(R.id.send); 

btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Firebase ref = new Firebase(FirebaseConfig.config);

//Getting values to store 
String name = etname.getText().toString().trim();
String password = etpassword.getText().toString().trim();

//Creating Person object
final PersonDetails person = new PersonDetails();
person.setName(name);
person.setPassword(password);

//Storing values to firebase 
ref.child("Person").push().setValue(person);

// Retrieve Data
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
 for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
  for (DataSnapshot postChildSnapshot : postSnapshot.getChildren()) {
    PersonDetails person = postChildSnapshot.getValue(PersonDetails.class);
    Log.i("MYTAG", "Person : "+ person.getName()+ "\n Password :- "+person.getPassword());
}}
}

@Override
public void onCancelled(FirebaseError firebaseError) {
//Error
}

});

}


Main.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout>

<EditText 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/name" 
android:hint="Enter Name"/> 

<EditText 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/password" 
android:hint="Enter password" 
android:layout_below="@+id/name" 
android:layout_alignParentStart="true" 
android:layout_marginTop="27dp" /> 

<Button 
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:text="Submit" 
android:id="@+id/send" 
android:layout_below="@+id/password" 
android:layout_alignParentStart="true" /> 

</RelativeLayout> 


Give Internet permission to your project through your manifest file. 
Manifest.xml 
<uses-permission android:name="android.permission.INTERNET" /> 

Comments