Pooveshin
Pooveshin

Reputation: 213

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());
                }
            });

        }
    });
}

Upvotes: 1

Views: 7945

Answers (3)

Vishal Kumar
Vishal Kumar

Reputation: 71

To avoid overwriting of data we can use push() method in following way-

from your code -

ref.child("Person").setValue(person);

just put push() before setValue() method, that is -

ref.child("Person").push().setValue(person);

It will avoid overwriting and it will create a unique id for each data you store under the Person.

Upvotes: 1

rhari
rhari

Reputation: 1367

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.

Upvotes: 3

Sergey Emeliyanov
Sergey Emeliyanov

Reputation: 6961

in Firebase you can not store many items/values under one child. But, you can make as many childs (children) with a single value as you want. Just make childs with tags s.a. "address" and "name" with their respective values.

Upvotes: 1

Related Questions