firebase.database.DatabaseException:无法将java.lang.Long类型的值转换为String

时间:2016-12-31 08:30:13

标签: android firebase firebase-realtime-database firebaseui

[This is the database]

firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String

我正在尝试将此数据添加到FirebaserecyclerAdapter并收到此错误

这是数据库参考

    databaseReference = FirebaseDatabase.getInstance().getReference().child("questions");

这就是我使用RecyclerViewAdapter的方式

  FirebaseRecyclerAdapter<History, HistoryViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<History, HistoryViewHolder>(
            History.class, R.layout.history_list_item, HistoryViewHolder.class, databaseReference) {
        @Override
        protected void populateViewHolder(HistoryViewHolder viewHolder, History model, int position) {
            viewHolder.setMyanswer(model.getMyAnswer());
            viewHolder.setCorrentAnswer(model.getCorrectAnswer());
            viewHolder.setImageUrl(getApplicationContext(), model.getImageUrl());

        }
    };
    recyclerView.setAdapter(firebaseRecyclerAdapter);

2 个答案:

答案 0 :(得分:7)

数据库中的两个节点确实存在数据类型不匹配的情况:

myAnswer : "21"
myAnswer : 11

如果你想让它成为一个字符串,那么它应该是:

myAnswer : "21"
myAnswer : "11"

或者,让他们长久:

myAnswer : 21
myAnswer : 11

确保在添加数据时注意一致性

答案 1 :(得分:0)

valueX = 21.4;

valueY = 23.5;

需要将Long更改为String

String mValuex = String.valueOf(valueX);

String mValuey = String.valueOf(valueY);

myRef.child(username).child("valuex").setValue(mValuex);

myRef.child(username).child("valuey").setValue(mValuey);