Friday, 19 August 2016

FireBase Data Storage and Retrival .Real Time Update of Recycler View

Declare FireBase dependencies in Gradle Files and do necessary steps for FireBase Setup.

I'm gonna write only the coding part.

//Declare this in oncreate of your activity
Firebase.setAndroidContext(this);

//Get a FireBase Reference Change URL a/c to your Firebase project(Check it in Firebase console)
Firebase ref = new Firebase("https://birthday-2599.firebaseio.com/");



//Onclick of the button save data to FireBase

Person person = new Person();

String name = nameEd.getText().toString().trim();
String phNo = number.getText().toString().trim();
String dob = tv.getText().toString().trim();
//Adding valuesif(!(name.equals("") || phNo.equals("") || dob.equals(""))) {
    person.setName(name);
    person.setPhNo(Integer.parseInt(phNo));
    person.setDob(dob);
    FirebaseHelper.save(person);
    finish();
}




//Save method

//SAVEpublic static Boolean save(Person person) {
    if (person == null) {
        saved = false;
    } else {

        ref.push().setValue(person);

        saved = true;

    }
    return saved;
}




//Retrive
//Set valuechange event listener and update data in RecyclerView


ref.addValueEventListener(new ValueEventListener() {

    @Override    public void onDataChange(DataSnapshot snapshot) {
//Clear array which your are passing in the adapter
        displayArray.clear();
        for (DataSnapshot postSnapshot : snapshot.getChildren()) {
            //Getting the data from snapshot            Person person = postSnapshot.getValue(Person.class);

            //Adding it to a string            String string = person.toString();


            displayArray.add(string);
            RecyclerView.Adapter adapter = new DataAdapter(displayArray);
            recyclerView.setAdapter(adapter);


        }
    }

    @Override    public void onCancelled(FirebaseError firebaseError) {
        System.out.println("The read failed: " + firebaseError.getMessage());
    }


});


ProjectFiles


Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...