Home > Software design >  Remove curly braces and equal sign from data retrieved from firebase
Remove curly braces and equal sign from data retrieved from firebase

Time:02-14

So, I am using android studio to make an app that displays data stored in firebase real-time database. The app is simple, it displays the name and phone number from firebase to the app.

The app works fine and even displays the data but the only thing is it displays the data/values with curly braces and an equal sign (like a JSON format) is there anyway I can make it display just the desired values and not the extra signs and punctuation

Image of the app screen with the issue is attached

Code:

ArrayList<String> list =new ArrayList<>();
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_view, list);
listView.setAdapter(adapter);

DatabaseReference ref= FirebaseDatabase.getInstance().getReference().child("Car Wash");

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        list.clear();
        for(DataSnapshot snapshot: dataSnapshot.getChildren()){
            list.add(snapshot.getValue().toString());
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

CodePudding user response:

What you're seeing is the toString() output of the value a Firebase DataSnapshot that represents an object with multiple values.

You'll want to get the individual values from that snapshot, and display their values with something like this:

String name = snapshot.child("Name").getValue(String.class);
String phoneNr = snapshot.child("Phone Number").getValue(String.class);
list.add(name " (" phoneNr ")");
  • Related