1

I have the following code trying to retrieve firebase values. My database structure is as follows:

enter image description here

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mystatement);
    listview = (ListView) findViewById(R.id.listview);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, list);
    listview.setAdapter(adapter);
    dref = FirebaseDatabase.getInstance().getReference();
    dref = dref.child("Dog Expenditure");

    dref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                HashMap<String, Object> value = (HashMap<String, Object>) child.getValue();
                String amount = value.get("amount").toString();
                String item = value.get("item").toString();
                // This will print out amount and item
                System.out.println(amount + item);
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

I have tried different ways to do this but i do not just get any luck here..All i get is this error java.lang.ClassCastException: java.lang.String cannot be cast to java.util.HashMap at com.example.moses.farm.Mystatement$1.onDataChange(Mystatement.java:39) which points to this HashMap value = (HashMap) child.getValue(); line of code. I have been stuck here for almost a week, anyone with an idea of how to get this will truly help me

2
  • May be your child return String please check it... Commented Mar 6, 2017 at 12:15
  • Not sure if this will fix it, but you should definitely not cast to a HashMap. Instead cast: Map<String, Object> value = (Map<String, Object>) child.getValue() Commented Mar 6, 2017 at 14:37

5 Answers 5

2
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mystatement);
    listview = (ListView) findViewById(R.id.listview);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, list);
    listview.setAdapter(adapter);
    dref = FirebaseDatabase.getInstance().getReference();
    dref = dref.child("Dog Expenditure");

    dref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                String amount = child.get("amount").toString();
                String item = value.get("item").toString();
                // This will print out amount and item
                System.out.println(amount + item);
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

use this code

Sign up to request clarification or add additional context in comments.

3 Comments

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.HashMap
i have noticed something very strange..in my android studio console((monitor) i see the value of a firebase entry i added manually..i.e my structurre is like this Dog Expenditure ---KeT_eEhkkiKJYOo1hb5 -- amount: "12'000" --item: "gsgag" then i manually added a child called amount: "xxxx" still under Dog Expenditure but now without this unique identifier "KeT_eEhkkiKJYOo1hb5". The console prints out xxxx. but not the original values with a unique identifire
ok it means at least you are able to resolve type casting error right ?
1
HashMap<String, Object> value = child.getValue();

try this one

6 Comments

@ManfredRadlwimmer java.lang.ClassCastException: java.lang.String cannot be cast to java.util.HashMap
@SirGeorge I didn't write the answer, karthi did. I simply removed the in-bowser snipped markup - can't help you with that.
replace object to string
@karthi java.lang.ClassCastException: java.lang.String cannot be cast to java.util.HashMap
HashMap<String, String> value = (HashMap<String, String>) child.getValue();
|
1
DatabaseReference dref = FirebaseDatabase.getInstance().getReference();
dref = dref.child("Dog Expenditure");

dref.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        HashMap<String, String> value = (HashMap<String, String>) dataSnapshot.getValue();
        String amount = value.get("amount");
        String item = value.get("item");
    }
    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
    }
    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {
    }
    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});

1 Comment

please send me your retrieval code so i can try it with my app. i am strunded here
0

The correct way to do it is to first create a data model:

public class DogExpenditure {
    private String amount;
    private String item;

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }
}

Then add a ChildEventListener instead:

dref.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        DogExpenditure dogExpenditure = dataSnapshot.getValue(DogExpenditure.class);
        System.out.println(dogExpenditure.getAmount()+dogExpenditure.getItem());
    }

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

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

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

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

1 Comment

hey java.lang.ClassCastException: java.lang.String cannot be cast to java.util.HashMap errror
0

Change this line value.get("amount").toString();

TO

String.valueOf(value.get("amount")); It solved my problem.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.