英文:
com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String problem
问题
public class QuestionsActivity extends AppCompatActivity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
private TextView question, noIndicator;
private FloatingActionButton bookmarkbtn;
private LinearLayout optioncontainer;
private Button shareBtn, nextBtn;
private int count = 0;
private List<QuestionsModel> list;
private int position = 0;
private int score = 0;
private int setNo;
private String category;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
Toolbar toolbar = findViewById(R.id.toolbar11);
setSupportActionBar(toolbar);
question = findViewById(R.id.question);
noIndicator = findViewById(R.id.no_indicator);
bookmarkbtn = findViewById(R.id.bookmark_btn);
optioncontainer = findViewById(R.id.option_container);
shareBtn = findViewById(R.id.share_btn);
nextBtn = findViewById(R.id.next_btn);
category = getIntent().getStringExtra("category");
setNo = getIntent().getIntExtra("setNo", 1);
list = new ArrayList<>();
myRef.child("SETS").child(category).child("Questions").orderByChild("setNo").equalTo(setNo).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
list.add(snapshot.getValue(QuestionsModel.class));
}
if (list.size() > 0) {
for (int i = 0; i < 4; i++) {
optioncontainer.getChildAt(i).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer((Button) v);
}
});
}
playAnim(question, 0, list.get(position).getQuestion());
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextBtn.setEnabled(false);
nextBtn.setAlpha(0.7f);
enableoption(true);
position++;
if (position == list.size()) {
return;
}
count = 0;
playAnim(question, 0, list.get(position).getQuestion());
}
});
} else {
finish();
Toast.makeText(QuestionsActivity.this, "no questions", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(QuestionsActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void playAnim(final View view, final int value, final String data) {
view.animate().alpha(value).scaleX(value).scaleY(value).setDuration(500).setStartDelay(100).setInterpolator(new DecelerateInterpolator()).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
if (value == 0 && count < 4) {
String option = "";
if (count == 0) {
option = list.get(position).getOptionA();
} else if (count == 1) {
option = list.get(position).getOptionB();
} else if (count == 2) {
option = list.get(position).getOptionC();
} else if (count == 3) {
option = list.get(position).getOptionD();
}
playAnim(optioncontainer.getChildAt(count), 0, option);
count++;
}
}
@Override
public void onAnimationEnd(Animator animation) {
if (value == 0) {
try {
((TextView) view).setText(data);
noIndicator.setText(position + 1 + "/" + list.size());
} catch (ClassCastException ex) {
((Button) view).setText(data);
}
view.setTag(data);
playAnim(view, 1, data);
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
private void checkAnswer(Button selectedoption) {
enableoption(false);
nextBtn.setEnabled(true);
nextBtn.setAlpha(1);
if (selectedoption.getText().toString().equals(list.get(position).getCorrectAns())) {
score++;
selectedoption.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#4CAF50")));
} else {
selectedoption.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#ff0000")));
Button correctoption = optioncontainer.findViewWithTag(list.get(position).getCorrectAns());
correctoption.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#4CAF50")));
}
}
private void enableoption(boolean enable) {
for (int i = 0; i < 4; i++) {
optioncontainer.getChildAt(i).setEnabled(enable);
if (enable) {
optioncontainer.getChildAt(i).setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#989898")));
}
}
}
}
public class QuestionsModel {
private String optionA, optionB, optionC, optionD, correctAns;
private int setNo;
private String question;
public QuestionsModel() {
}
public QuestionsModel(String question, String optionA, String optionB, String optionC, String optionD, String correctAns, int setNo) {
this.setNo = setNo;
this.question = question;
this.optionA = optionA;
this.optionB = optionB;
this.optionC = optionC;
this.optionD = optionD;
this.correctAns = correctAns;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getOptionA() {
return optionA;
}
public void setOptionA(String optionA) {
this.optionA = optionA;
}
public String getOptionB() {
return optionB;
}
public void setOptionB(String optionB) {
this.optionB = optionB;
}
public String getOptionC() {
return optionC;
}
public void setOptionC(String optionC) {
this.optionC = optionC;
}
public String getOptionD() {
return optionD;
}
public void setOptionD(String optionD) {
this.optionD = optionD;
}
public String getCorrectAns() {
return correctAns;
}
public void setCorrectAns(String correctAns) {
this.correctAns = correctAns;
}
public int getSetNo() {
return setNo;
}
public void setSetNo(int setNo) {
this.setNo = setNo;
}
}
英文:
I have a quiz app
i add the Question on my firebase Realtie Database
when i want to see it show me this error
> com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String"
in this page :
> com.quiz.footwikiquiz.questions.QuestionsActivity$1.onDataChange(QuestionsActivity.java:72)
heres my code for QuestionsActivity :
public class QuestionsActivity extends AppCompatActivity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
private TextView question,noIndicator;
private FloatingActionButton bookmarkbtn;
private LinearLayout optioncontainer;
private Button shareBtn,nextBtn;
private int count=0;
private List<QuestionsModel> list;
private int position =0;
private int score=0;
private int setNo;
private String category;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
Toolbar toolbar = findViewById(R.id.toolbar11);
setSupportActionBar(toolbar);
question = findViewById(R.id.question);
noIndicator = findViewById(R.id.no_indicator);
bookmarkbtn = findViewById(R.id.bookmark_btn);
optioncontainer = findViewById(R.id.option_container);
shareBtn = findViewById(R.id.share_btn);
nextBtn = findViewById(R.id.next_btn);
category = getIntent().getStringExtra("category");
setNo = getIntent().getIntExtra("setNo",1);
list = new ArrayList<>();
myRef.child("SETS").child(category).child("Questions").orderByChild("setNo").equalTo(setNo).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
list.add(snapshot.getValue(QuestionsModel.class));
}
if (list.size() > 0) {
for (int i=0 ; i<4 ;i++) {
optioncontainer.getChildAt(i).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer((Button)v);
}
});
}
playAnim(question,0,list.get(position).getQuestion());
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextBtn.setEnabled(false);
nextBtn.setAlpha(0.7f);
enableoption(true);
position++;
if (position == list.size()) {
return;
}
count= 0;
playAnim(question,0,list.get(position).getQuestion());
}
});
}else {
finish();
Toast.makeText(QuestionsActivity.this, "no questions", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(QuestionsActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void playAnim(final View view , final int value , final String data) {
view.animate().alpha(value).scaleX(value).scaleY(value).setDuration(500).setStartDelay(100).setInterpolator(new DecelerateInterpolator()).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
if (value ==0 && count<4) {
String option ="";
if (count == 0) {
option = list.get(position).getOptionA();
} else if (count == 1) {
option = list.get(position).getOptionB();
} else if (count == 2) {
option = list.get(position).getOptionC();
} else if (count == 3) {
option = list.get(position).getOptionD();
}
playAnim(optioncontainer.getChildAt(count),0,option);
count++;
}
}
@Override
public void onAnimationEnd(Animator animation) {
if (value ==0 ) {
try {
((TextView)view).setText(data);
noIndicator.setText(position+1+"/"+list.size());
}catch (ClassCastException ex){
((Button)view).setText(data);
}
view.setTag(data);
playAnim(view,1,data);
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
private void checkAnswer(Button selectedoption) {
enableoption(false);
nextBtn.setEnabled(true);
nextBtn.setAlpha(1);
if (selectedoption.getText().toString().equals(list.get(position).getCorrectAns())) {
score++;
selectedoption.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#4CAF50")));
} else {
selectedoption.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#ff0000")));
Button correctoption = optioncontainer.findViewWithTag(list.get(position).getCorrectAns());
correctoption.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#4CAF50")));
}
}
private void enableoption(boolean enable){
for (int i=0 ; i<4 ;i++) {
optioncontainer.getChildAt(i).setEnabled(enable);
if (enable) {
optioncontainer.getChildAt(i).setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#989898")));
}
}
}
}
and this is the QuestionModel code :
public class QuestionsModel {
private String optionA, optionB, optionC, optionD, correctAns;
private int setNo;
private String question;
public QuestionsModel(){
}
public QuestionsModel(String question, String optionA, String optionB, String optionC, String optionD, String correctAns,int setNo) {
this.setNo = setNo;
this.question = question;
this.optionA = optionA;
this.optionB = optionB;
this.optionC = optionC;
this.optionD = optionD;
this.correctAns = correctAns;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getOptionA() {
return optionA;
}
public void setOptionA(String optionA) {
this.optionA = optionA;
}
public String getOptionB() {
return optionB;
}
public void setOptionB(String optionB) {
this.optionB = optionB;
}
public String getOptionC() {
return optionC;
}
public void setOptionC(String optionC) {
this.optionC = optionC;
}
public String getOptionD() {
return optionD;
}
public void setOptionD(String optionD) {
this.optionD = optionD;
}
public String getCorrectAns() {
return correctAns;
}
public void setCorrectAns(String correctAns) {
this.correctAns = correctAns;
}
public int getSetNo() {
return setNo;
}
public void setSetNo(int setNo) {
this.setNo = setNo;
}
}
this is my firebase
答案1
得分: 0
你将答案(optionA
、optionB
等)存储为 Question1
中的数字,但你尝试将它们转换为字符串。
如果你直接访问 DataSnapshot
值,可以通过以下方式获取对象:
myRef.child("SETS").child(category).child("Questions").orderByChild("setNo").equalTo(setNo).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Log.i("Database", snapshot.child("optionA").getValue());
}
}
});
如果你想将选项放入你的 Java 类中,唯一能够做到的方式是将所有答案声明为 Object
:
public class QuestionsModel {
private Object optionA, optionB, optionC, optionD, correctAns;
private int setNo;
private String question;
public QuestionsModel(){
}
public QuestionsModel(String question, String optionA, String optionB, String optionC, String optionD, String correctAns,int setNo) {
this.setNo = setNo;
this.question = question;
this.optionA = optionA;
this.optionB = optionB;
this.optionC = optionC;
this.optionD = optionD;
this.correctAns = correctAns;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public Object getOptionA() {
return optionA;
}
public void setOptionA(Object optionA) {
this.optionA = optionA;
}
public Object getOptionB() {
return optionB;
}
public void setOptionB(Object optionB) {
this.optionB = optionB;
}
public Object getOptionC() {
return optionC;
}
public void setOptionC(Object optionC) {
this.optionC = optionC;
}
public Object getOptionD() {
return optionD;
}
public void setOptionD(Object optionD) {
this.optionD = optionD;
}
public Object getCorrectAns() {
return correctAns;
}
public void setCorrectAns(Object correctAns) {
this.correctAns = correctAns;
}
public int getSetNo() {
return setNo;
}
public void setSetNo(int setNo) {
this.setNo = setNo;
}
}
但总的来说,我建议修复数据库中 Question1
的答案类型,将它们存储为字符串值。
英文:
You're storing the answers (optionA
, optionB
, etc) as numbers in Question1
, but you are trying to cast them to a string.
If you're directly accessing the DataSnapshot
value you can get the object with:
myRef.child("SETS").child(category).child("Questions").orderByChild("setNo").equalTo(setNo).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Log.i("Database", snapshot.child("optionA").getValue();
}
If you want to put the options into your Java class, the only way you can do that is by declaring all answers as Object
:
public class QuestionsModel {
private Object optionA, optionB, optionC, optionD, correctAns;
private int setNo;
private String question;
public QuestionsModel(){
}
public QuestionsModel(String question, String optionA, String optionB, String optionC, String optionD, String correctAns,int setNo) {
this.setNo = setNo;
this.question = question;
this.optionA = optionA;
this.optionB = optionB;
this.optionC = optionC;
this.optionD = optionD;
this.correctAns = correctAns;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public Object getOptionA() {
return optionA;
}
public void setOptionA(Object optionA) {
this.optionA = optionA;
}
public Object getOptionB() {
return optionB;
}
public void setOptionB(Object optionB) {
this.optionB = optionB;
}
public Object getOptionC() {
return optionC;
}
public void setOptionC(Object optionC) {
this.optionC = optionC;
}
public Object getOptionD() {
return optionD;
}
public void setOptionD(Object optionD) {
this.optionD = optionD;
}
public Object getCorrectAns() {
return correctAns;
}
public void setCorrectAns(Object correctAns) {
this.correctAns = correctAns;
}
public int getSetNo() {
return setNo;
}
public void setSetNo(int setNo) {
this.setNo = setNo;
}
}
But in general I'd recommend fixing the type of the answers to Question1
in the database, and storing them as string values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论