Java 如何将long或double int数据类型转换为字符串inFfirebase dataSnapshot
我已经使用Firebase数据库创建了实时测验应用程序,但当我从数据库中检索整数值时,它会显示错误Java 如何将long或double int数据类型转换为字符串inFfirebase dataSnapshot,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我已经使用Firebase数据库创建了实时测验应用程序,但当我从数据库中检索整数值时,它会显示错误 com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String at com.google.android.gms.internal.zzepg.zzb(Unknown Source) at com.google.android.gms.
com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
at com.google.android.gms.internal.zzepg.zzb(Unknown Source)
at com.google.android.gms.internal.zzepg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.ejobbox.ejobbox.SimpleQuiz$6.onDataChange(SimpleQuiz.java:164)
at com.google.android.gms.internal.zzejp.zza(Unknown Source)
at com.google.android.gms.internal.zzelk.zza(Unknown Source)
at com.google.android.gms.internal.zzelq.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
在com.ejobbox.ejobbox.SimpleQuiz$6.onDataChange(SimpleQuiz.java:164)
在这个应用程序中,我不能定义特定的数据类型,因为datatype datatype字段值将是string或integer。在检索数据后,我希望将其转换为string或integer,所以请让我知道如何执行此操作
我使用String.valueOf()转换数据,但它不起作用,因此请帮助我如何将数据转换为字符串。
String Option1=String.valueOf(dataSnapshot.getValue(String.class).toString());
SimpleQuiz.Java
package com.ejobbox.ejobbox;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class SimpleQuiz extends AppCompatActivity {
private TextView mScoreView;
private TextView mQuestionView;
private Button mButtonChoice1;
private Button mButtonChoice2;
private Button mButtonChoice3;
private Button mButtonChoice4;
private String mAnswer;
private int mScore = 0;
private int mQuestionNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_quiz);
mScoreView = (TextView)findViewById(R.id.score);
mQuestionView = (TextView)findViewById(R.id.question);
mButtonChoice1 = (Button)findViewById(R.id.choice1);
mButtonChoice2 = (Button)findViewById(R.id.choice2);
mButtonChoice3 = (Button)findViewById(R.id.choice3);
mButtonChoice4 = (Button)findViewById(R.id.choice4);
updateQuestion();
//Start of Button Listener for Button1
mButtonChoice1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice1.getText().equals(mAnswer)){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//This line of code is optiona
Toast.makeText(SimpleQuiz.this, "correct", Toast.LENGTH_SHORT).show();
mButtonChoice1.setBackgroundColor(Color.parseColor("#29b200"));
}else {
mButtonChoice1.setBackgroundColor(Color.parseColor("#c10000"));
Toast.makeText(SimpleQuiz.this, "wrong", Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
//End of Button Listener for Button1
//Start of Button Listener for Button2
mButtonChoice2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice2.getText().equals(mAnswer)){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//This line of code is optiona
mButtonChoice2.setBackgroundColor(Color.parseColor("#29b200"));
Toast.makeText(SimpleQuiz.this, "correct", Toast.LENGTH_SHORT).show();
}else {
mButtonChoice2.setBackgroundColor(Color.parseColor("#c10000"));
Toast.makeText(SimpleQuiz.this, "wrong", Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
//End of Button Listener for Button2
//Start of Button Listener for Button3
mButtonChoice3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice3.getText().equals(mAnswer)){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//This line of code is optiona
mButtonChoice3.setBackgroundColor(Color.parseColor("#29b200"));
Toast.makeText(SimpleQuiz.this, "correct", Toast.LENGTH_SHORT).show();
}else {
mButtonChoice3.setBackgroundColor(Color.parseColor("#c10000"));
Toast.makeText(SimpleQuiz.this, "wrong", Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
//End of Button Listener for Button3
mButtonChoice4.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice4.getText().equals(mAnswer)){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//This line of code is optiona
mButtonChoice4.setBackgroundColor(Color.parseColor("#29b200"));
Toast.makeText(SimpleQuiz.this, "correct", Toast.LENGTH_SHORT).show();
}else {
mButtonChoice1.setBackgroundColor(Color.parseColor("#c10000"));
Toast.makeText(SimpleQuiz.this, "wrong", Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
}
private void updateQuestion(){
DatabaseReference mQuestion1 = FirebaseDatabase.getInstance().getReference();
DatabaseReference gQuestion1=mQuestion1.child(mQuestionNumber+"/question");
gQuestion1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question1=String.valueOf(dataSnapshot.getValue(String.class).toString());
mQuestionView.setText(question1);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
DatabaseReference gOption1=mDatabase.child(mQuestionNumber+"/choice1");
gOption1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String Option1=String.valueOf(dataSnapshot.getValue(String.class).toString());
mButtonChoice1.setText(Option1);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference gOption2=mDatabase.child(mQuestionNumber+"/choice2");
gOption2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String Option2=String.valueOf(dataSnapshot.getValue(String.class).toString());
mButtonChoice2.setText(Option2);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference gOption3=mDatabase.child(mQuestionNumber+"/choice3");
gOption3.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String Option3=String.valueOf(dataSnapshot.getValue(String.class).toString());
mButtonChoice3.setText(Option3);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference gOption4=mDatabase.child(mQuestionNumber+"/choice4");
gOption4.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String Option4=String.valueOf(dataSnapshot.getValue(String.class).toString());
mButtonChoice4.setText(Option4);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference mQAnswer=mDatabase.child(mQuestionNumber+"/answer");
mQAnswer.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mAnswer=String.valueOf(dataSnapshot.getValue(String.class));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mQuestionNumber++;
}
private void updateScore(int point) {
mScoreView.setText("" + mScore);
}
}
数据库结构
{
"0" : {
"answer" : "President",
"choice1" : "PM",
"choice2" : "AM",
"choice3" : "President",
"choice4" : "Vice President",
"question" : "who is Ramnath kovind ?"
},
"1" : {
"answer" : "Red",
"choice1" : "Red",
"choice2" : "Green",
"choice3" : "Black",
"choice4" : "Yellow",
"question" : "Rose color is___?"
},
"2" : {
"answer" : 3,
"choice1" : 5,
"choice2" : 4,
"choice3" : 3,
"choice4" : 2,
"question" : "How many color in indian flage ?"
},
"UpdateVersion" : 1,
"apkUrls" : "https://www.ejobbox.com"
}
要解决此问题,请更改此代码行:
String Option1=String.valueOf(dataSnapshot.getValue(String.class).toString());
ref.child("choice1").setValue(5);
与
请记住,dataSnapshot.getValue(Long.class)
返回一个Long
,它是基本体,您不能对基本体类型调用toString()
因为选项1
包含不同类型的对象,为了解决这个问题,OP决定将所有值都作为字符串。因此,不要使用以下代码行:
String Option1=String.valueOf(dataSnapshot.getValue(String.class).toString());
ref.child("choice1").setValue(5);
我们应该利用:
use ref.child("choice1").setValue("5");
使用引号
,值将存储为字符串。最后,要使其正常工作,只需删除测试数据并添加新数据。三种方法是可行的:
第一:这是一个很简单的方法,但我不喜欢
String option1 = dataSnapshot.getValue() + ""; //it it works
第二:您需要检查正在访问的数据的每种数据类型,并相应地转换它们
if ((dataSnapshot.getValue()) instanceof Long )
{
String Option1=String.valueOf(dataSnapshot.getValue(Long.class));
}
else if ((dataSnapshot.getValue()) instanceof Double )
{
String Option1=String.valueOf(dataSnapshot.getValue(Double.class));
}
else if (...)
{
//and so on
}
第三:您可以将数据库中的所有数据都作为字符串。怎么做?只需在每个数据后面添加任何字母。比如说,
"choice1" : 5,
"choice2" : 4,
"choice3" : 3,
"choice4" : 2,
将成为
"choice1" : 5L,
"choice2" : 4L,
"choice3" : 3L,
"choice4" : 2L,
这样,每个数据都将是“字符串”类型。然后使用
String Option1 = String.valueOf(dataSnapshot.getValue(String.class));
int indexno = Option1.indexOf("L");
Option1 = Option1.substring(0,indexno); //this line will remove "L" from the string
看到您的数据库,我可以告诉您,其中包含错误的值。您需要使choice1
属性的值在每个对象中都相同。在第一个对象中,值是“PM”
,它是一个字符串;在第二个对象中,值是“Red”
,它也是一个字符串;第三个对象中,值是5
,它是一个长
。为了解决这个问题,删除所有这些数据并添加新数据,这样您就可以在同一类型的所有对象中拥有所有值。如果所有值的类型都是Long
,则我的解决方案将有效。如果您决定使用字符串,那么只需使用:stringoption1=dataSnapshot.getValue(String.class)代码>好吗?告诉我它是否工作。它现在不工作。它向我显示错误“未能将java.lang.String类型的值转换为long”。在测验中,所有时间都没有特定的数据。请在某个时间添加年份和某个时间日期,以便如何处理它。管理所有类型数据的任何globel数据类型。现在我该怎么解决呢?请帮帮我。你读了我刚才写的吗?首先从数据库中删除这些项目,因为它们是错误的。如果数据库中有这些数据,则无法解决此问题。然后添加新数据。如果choice1
属性的所有值都是Long类型,那么我的解决方案就可以工作了。如果是字符串,那么只需使用:stringoption1=dataSnapshot.getValue(String.class)代码>有效?在测验中,并非所有时间都有特定的数据—有些时间加上年份和一些时间日期,那么如何处理它。管理所有类型数据的任何全局数据类型。一个字段不应有多个值类型。我认为最好的选择是将长值保存为字符串。