Ad

Android Firebase Failed To Convert Value Of Type Java.util.HashMap To String

I'm trying to use the listview using array adapter on my fragment but when trying to do it using the tutorial, It crashes every time i open that specific fragment. I'm trying to show the data on my firebase into the listview but it crashes every time i open it.

Here's my database enter image description here

From the picture My plan is to only show the email and name of the user and not to include the type but I'll do that later.

And here's my code

public class memberFragment extends Fragment {
private ListView mylistView;
private DatabaseReference mDatabase;
ArrayList<String> myArrayList = new ArrayList<>();
FirebaseDatabase myFirebase;


public memberFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_member2, container, false);
    mDatabase = FirebaseDatabase.getInstance().getReference().child("Accounts").child("Users");
    final ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,myArrayList);
     mylistView = (ListView)view.findViewById(R.id.listView);
     mylistView.setAdapter(myArrayAdapter);


    mDatabase.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String myChildValues = dataSnapshot.getValue(String.class);
            myArrayList.add(myChildValues);
            myArrayAdapter.notifyDataSetChanged();


        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            myArrayAdapter.notifyDataSetChanged();

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    // Inflate the layout for this fragment
    return view;

Any ideas what might be causing the problem? I tried to searched and found out its related to hashmap or map, but I have no idea how to put it on my array?

Ad

Answer

The can use a model class in order to achieve this or you can use a simplest way, using the String class. For that, you can use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Accounts").child("Users");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String email = ds.child("email").getValue(String.class);
            String name = ds.child("name").getValue(String.class);
            Log.d("TAG", email + " / " + name);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);

Your output will be:

[email protected] / idk
//and so on
Ad
source: stackoverflow.com
Ad