Pooveshin Pooveshin - 11 days ago 6
Android Question

Write multiple items to firebase in Android

I've written an android app using Firebase as the backend. My problem is only one item can be stored in Firebase and when I try to write more data it replaces the existing data. I want to write multiple bits and data and retrieve them in a list view or something.

My Code is below:

public class Capture_Meetings extends AppCompatActivity {

private EditText editTextName;
private EditText editTextAddress;
private EditText editDateTime;
private TextView textViewPersons;
private Button buttonSave;

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

Firebase.setAndroidContext(this);

buttonSave = (Button) findViewById(R.id.buttonSave);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextAddress = (EditText) findViewById(R.id.editTextAddress);
editDateTime = (EditText) findViewById(R.id.editDateTime);

textViewPersons = (TextView) findViewById(R.id.textViewPersons);

buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creating firebase object
Firebase ref = new Firebase(Config.FIREBASE_URL);

//Getting values to store
String name = editTextName.getText().toString().trim();
String address = editTextAddress.getText().toString().trim();
String DateTime = editDateTime.getText().toString().trim();

//Creating Person object
final MeetingUser person = new MeetingUser();

//Adding values
person.setName(name);
person.setAddress(address);
person.setDateTime(DateTime);

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


//Value event listener for realtime data update
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot)
{
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
//Getting the data from snapshot
MeetingUser person = postSnapshot.getValue(MeetingUser.class);

//Adding it to a string
String string = "\n Name of School: "+person.getName()+"\n Date:"+ person.getDateTime()+"\nMeeting Notes: "+person.getAddress()+"\n\n";

//Displaying it on textview
textViewPersons.setText(string);
}
}

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

}
});
}

Answer

Have a look at the Firebase documentation. You can use the push method to append data to a node, which will have a unique id.