F.joel
F.joel

Reputation: 143

android how to insert array data into firebase database

i followed this google firebase guide to insert array of data in database. what i have done is to create below code and it works fine:

 memberDatabaseReference= FirebaseDatabase.getInstance().getReference("member");

 save.setOnClickListener(new View.OnClickListener() {
        Map<String, String> member = new HashMap<>();
        @Override
        public void onClick(View view) {

       for(int i=1; i<5;i++){
                String value= "member "+String.valueOf(i);
                String id= memberDatabaseReference.push().getKey();
                  member.put("id", id);
                  member.put("name", value);
                sampleArray sampleArray = new sampleArray(member);
                memberDatabaseReference.child(id).setValue(sampleArray);
            }
        }
    });

Model Class

 public class sampleArray {

    String id;
    Map<String,String> samplearray;

    public Map<String, String> getSamplearray() {
        return samplearray;
    }

    public void setSamplearray(Map<String, String> samplearray) {
        this.samplearray = samplearray;
    }

    public sampleArray(Map<String,String> samplearray) {
        this.samplearray = samplearray;

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

}

db results are here

db results are here

Basically what i want is something like this,

and not this

---- autogenerated ID

---sampleArray

--- id:XXXXX

---member:XXXXX

How can I remove this string "sample array" because what I know is from model class but I don't know why it appears there. OR if there any trick on array concept to achieve that.

Upvotes: 1

Views: 2719

Answers (2)

Gast&#243;n Saill&#233;n
Gast&#243;n Saill&#233;n

Reputation: 13129

You can work with hashmaps, just do this in order to achieve what you want. So just change this line:

memberDatabaseReference.child(id).setValue(sampleArray);

to this:

memberDatabaseReference.child(id).updateChildren(member);

Remember that:

  • setValue method is totally replacing the document (specified reference) with new data

  • updateChildren method is just updating particular fields or add such fields if they did not exist before.

Upvotes: 0

Alex Mamo
Alex Mamo

Reputation: 138824

First off all, the link in your question is about Cloud Firestore and not Firebase Realtime database. But, in order to achieve a database structure that looks like this:

Firebase-root
    |
    --- member
          |
          --- autogenerated ID
                   |
                   --- id: "XXXXX"
                   |
                   --- name: "XXXXX"

Please use the following model class:

public class MemberModel {
    private String id, name;

    public MemberModel() {}

    public MemberModel(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() { return id; }
    public String getName() { return name; }
}

And to add data to the database, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference memberRef = rootRef.child("member");
String id = memberRef.push().getKey();
Member member = new Member("member 1", id);
memberRef.child(id).setValue(member);

Upvotes: 2

Related Questions