[Android Software Development] Geeks Empire:
Google Cloud & Firebase [Data Transferring]

​

Now It is time to Focus on Coding. It is not difficult to understand Firebase API But Using URL carefully to Create New User, Data, Section & etc is important.

Open MainActivity.Java

At First the Class must extended from Fragment that required for Firebase API (& later for Google Sign-In). In this case I use AppCompatActivity.

public class MainActivity extends AppCompatActivity{}

To work with Firebase API you have to define it like other objects.

You can do it when you need it Or define it at first part of app onCreate(Bundle){}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
[B]Firebase.setAndroidContext(getApplicationContext());[/B]

After that you need to declare instance of your views. For Example I declare ListView that will use for showing loaded Data.

ListView listView = (ListView)findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1);
//built-in adaptor
listView.setAdapter(adapter);

NOTE: Save your Firebase URL to <resource> <string /></resource> Or Save it as Universal String Object inside Class.

For Example Before onCreate(Bundle)

String FirebaseLink = "https://[B]YOUR_APP_NAME[/B].firebaseio.com/";
String FirebaseTitle = "GeeksEmpire";
String FirebaseSub = "gX";

To Get Data from User you should define input method like <EditText/>

With This methods you can Save Data Locally in Application Private Storage on System

//Save Content
try {
FileOutputStream fOut = openFileOutput(NAME, MODE_PRIVATE);
fOut.write((DATA).getBytes());

//Always Close All Streams
fOut.close();
fOut.flush();
}
catch (Exception e) {
System.out.println(e);
} finally {}

//Read-File
public String READ(String S, Context context){
File Fav = new File(S);
String Read = null;
FileInputStream fin;
BufferedReader br = null;
try{
fin = new FileInputStream(Fav);
br = new BufferedReader(new InputStreamReader(fin, "UTF-8"), 1024);
System.out.println(br);
int c;
String temp = "";
while( (c = br.read()) != -1){temp = temp + Character.toString((char)c);}
Read = temp;}
catch(Exception e)
{System.out.println(e);}
finally{try {br.close();} catch (IOException e) {e.printStackTrace();}}
return Read;
}

But to Store Data on Firebase Server you should declare instance of Firebase API, Push & setValue(DATA)

upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String DATA = editText.getText().toString();//"[COLOR=Red][I]GeeksEmpire DataBase on FireBase[/I][/COLOR]"
new Firebase([B]FirebaseLink[/B]/*[COLOR=Red][I]https://YOUR_APP_NAME.firebaseio.com[/I]/[/COLOR]*/ + FirebaseTitle/*[COLOR=Red][I]GeeksEmpire[/I][/COLOR]*/)
.push()
.child(FirebaseSub/*[COLOR=Red][I]gX[/I][/COLOR]*/)
.setValue(DATA);
}
});

Compare red texts with image. Firebase URL is submitted but you can Modify both FirebaseTitle & FirebaseSub.

In this Case I used GeeksEmpire as Title & gX as SubTitle & then Data.

It is depend on your design.

you can set GeeksEmpire as User & gX as Title OR Whatever you need.
​

Data Uploaded to Server & Now you should make an option to Download them. I created function to gather all components.

public void SetUpCloudContent(){
Firebase.setAndroidContext(this);
new Firebase(FirebaseLink/*[COLOR=Red]https://YOUR_APP_NAME.firebaseio.com/[/COLOR]*/ + FirebaseTitle/*[COLOR=Red]GeeksEmpire[/COLOR]*/).addChildEventListener(new ChildEventListener() {
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
adapter.add((String)dataSnapshot.child(FirebaseSub/*[COLOR=Red]gX[/COLOR]*/).getValue());//add each value to ListView
}
public void onChildRemoved(DataSnapshot dataSnapshot) {
adapter.remove((String)dataSnapshot.child(FirebaseSub).getValue());
}
public void onChildChanged(DataSnapshot dataSnapshot, String s) { }
public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
public void onCancelled(FirebaseError firebaseError) { }
});
}

To Change Data Just Use ID you got to User (For Example GeeksEmpire > gX) and OverWrite Data.

But here is a method to Delete Data.

// Delete items when clicked

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
new Firebase([COLOR=Red]FirebaseLink [/COLOR]+ [COLOR=Red]FirebaseTitle[/COLOR])
.orderByChild([COLOR=Red]FirebaseSub[/COLOR])
.equalTo((String) listView.getItemAtPosition(position)/*[I]get data from ListView[/I]*/)
.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren()) {
DataSnapshot firstChild = dataSnapshot.getChildren().iterator().next();
firstChild.getRef().removeValue();
}
}
public void onCancelled(FirebaseError firebaseError) { }
});
return false;
}
});

Sending & Receiving Data to Firebase Cloud Service is Done.

Again It depends on you How to define Users & Create Individual ID for each one.

Next Posts Will Show How to Create Sign-In Options for your App. That Also Help a lot to create Individual ID.

Thanks for Supporting GeeksEmpire projects

​

Don't Forget To Hit Thanks
​