Mithu
Mithu

Reputation: 655

How to Check Data is Inserted Successfully in Firebase in Array

I am inserting some data in Firebase database From json Response in an array with below code

JSONArray arr = new JSONArray(result);
String[] stocks = new String[arr.length()];

for(int i=0;i<arr.length();i++){

   JSONObject obj = arr.getJSONObject(i);
   mDatabase= FirebaseDatabase.getInstance().getReference().child("books");
   atabaseReference newBid=mDatabase.push();
   newBid.child("usr_id").setValue(obj.getString("user_id"));
   newBid.child("usr_fullNme").setValue(obj.getString("first_name")+" "+obj.getString("last_name"));
   newBid.child("usr_mobile").setValue(obj.getString("user_mobile"));
   newBid.child("usr_avatr").setValue(obj.getString("src"));
  }

How can i check if above operation is successful or not

Upvotes: 0

Views: 725

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80924

You can use a Hashmap and do the following:

Map<String, Object> userValues = new HashMap<>();
  userValues.put("usr_id", obj.getString("user_id"));
  userValues.put("usr_fullNme",obj.getString("first_name")+" "+obj.getString("last_name"));
  userValues.put("usr_mobile", obj.getString("user_mobile"));
  userValues.put("usr_avatr",  obj.getString("src"));

Then use setValue():


mDatabase= FirebaseDatabase.getInstance().getReference().child("books");
String newBid = mDatabase.push().getKey();
mDatabase.child(newBid).setValue(userValues, new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
           System.out.println(databaseError);
        }
    });

From the docs:

public void setValue (Object value, DatabaseReference.CompletionListener listener)

Set the data at this location to the given value. Passing null to setValue() will delete the data at the specified location. The native types accepted by this method for the value correspond to the JSON types:

  • Boolean
  • Long
  • Double
  • String
  • Map
  • List

Upvotes: 2

Related Questions