gpt4 book ai didi

java - 如何将 long 或 double int 数据类型转换为 Ffirebase dataSnapshot 中的 String

转载 作者:行者123 更新时间:2023-12-02 11:25:55 26 4
gpt4 key购买 nike

我已经使用 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.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)

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

}
});

在此应用程序中,我无法定义特定的数据类型,因为数据类型数据类型字段值将是字符串或整数,希望在数据检索后对其进行转换,所以请让我知道如何执行此操作。

我使用 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());

String Option1=String.valueOf(dataSnapshot.getValue(Long.class));

请记住,dataSnapshot.getValue(Long.class) 返回一个 long,它是原始类型,您无法在该对象上调用 toString()原始类型。

由于 choice1 包含不同类型的对象,为了解决该问题,OP 决定将所有值都作为字符串。因此,不要使用以下代码行:

ref.child("choice1").setValue(5);

我们应该使用:

use ref.child("choice1").setValue("5");

使用引号,该值将存储为字符串。最后,要使其正常工作,只需删除测试数据并添加新数据即可。

关于java - 如何将 long 或 double int 数据类型转换为 Ffirebase dataSnapshot 中的 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49630902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com