Skip to content
Advertisement

Remove curly braces and equal sign from data retrieved from firebase

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) {

    }
});

Advertisement

Answer

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+")");
Advertisement