java.lang.String无法强制转换为java.util.HashMap错误

时间:2017-03-06 12:07:15

标签: android firebase firebase-realtime-database

我有以下代码尝试检索firebase值。我的数据库结构如下: 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) {

                }
            });
            }

我已尝试过不同的方法来做到这一点,但我不会在这里得到任何运气..我得到的是这个错误 java.lang.ClassCastException:java.lang.String无法强制转换为java.util。 HashMap中                                                                           在com.example.moses.farm.Mystatement $ 1.onDataChange(Mystatement.java:39),它指向此 HashMap值=(HashMap)child.getValue(); 代码行。我已经被困在这里将近一个星期,任何知道如何获得这个的人都会真正帮助我

5 个答案:

答案 0 :(得分:1)

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

            }
        });
        }

使用此代码

答案 1 :(得分:0)

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

        }
    });

答案 2 :(得分:0)

正确的方法是首先创建一个数据模型:

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;
    }
}

然后添加一个ChildEventListener:

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

    }
});

答案 3 :(得分:0)

HashMap<String, Object> value = child.getValue();

尝试这个

答案 4 :(得分:0)

更改此行value.get(&#34; amount&#34;)。toString();

将String.valueOf(value.get(&#34;量&#34)); 它解决了我的问题。