java - 在 TextView 上工作时,按钮上的 setText 不起作用
问题描述
这是我的 Firebase 数据库结构: 在此处输入图像描述
我对Java相当陌生,并试图弄清楚如何解决这个问题。所以我正在制作一个测验应用程序,直到现在一切正常,除了我无法从 Firebase 实时数据库中检索数据。
我的布局:我有一个 textView 来显示问题 4 个按钮来显示用户可以从中选择的选项 当用户单击按钮时,按钮颜色会根据问题答案变为红色或绿色,如果它是正确的或不。我为计时器添加了一个 textView,它被忽略了
我只能检索问题 textView 的数据,但按钮不显示任何内容
我的问题类:
package com.example.android.quizapp;
public class Question
{
public String question,option1,option2,option3,option4,answer;
public Question(String question,String option1,String option2,String option3,String option4,String answer)
{
this.question=question;
this.option1=option1;
this.option2=option2;
this.option3=option3;
this.option4=option4;
this.answer=answer;
}
public Question()
{
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getOption1() {
return option1;
}
public void setOption1(String option1) {
this.option1 = option1;
}
public String getOption2() {
return option2;
}
public void setOption2(String option2) {
this.option2 = option2;
}
public String getOption3() {
return option3;
}
public void setOption3(String option3) {
this.option3 = option3;
}
public String getOption4() {
return option4;
}
public void setOption4(String option4) {
this.option4 = option4;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
我的问题java活动类:
package com.example.android.quizapp;
import android.app.VoiceInteractor;
import android.graphics.Color;
import android.graphics.Path;
import android.os.Handler;
import android.support.annotation.NonNull;
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 questions extends AppCompatActivity
{
TextView txtquestions,timer;
Button OptionA,OptionB,OptionC,OptionD;
int total=0;
int correct=0;
int wrong=0;
DatabaseReference reference;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
txtquestions=(TextView)findViewById(R.id.Questions);
OptionA=(Button)findViewById(R.id.OptionA);
OptionB=(Button)findViewById(R.id.OptionB);
OptionC=(Button)findViewById(R.id.OptionC);
OptionD=(Button)findViewById(R.id.OptionD);
timer=(TextView)findViewById(R.id.timer);
updateQuestions();
}
private void updateQuestions()
{
total++;
if(total>2)
{
//open the result activity
Toast.makeText(questions.this,"Done",Toast.LENGTH_SHORT).show();
}
else
{
reference=FirebaseDatabase.getInstance().getReference().child("questions").child(String.valueOf(total));
reference.addValueEventListener((new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
final Question question=dataSnapshot.getValue(Question.class);
txtquestions.setText(question.getQuestion());
OptionA.setText(question.getOption1());
OptionB.setText(question.getOption2());
OptionC.setText(question.getOption3());
OptionD.setText(question.getOption4());
OptionA.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(OptionA.getText().toString().equals(question.getAnswer()))
{
OptionA.setBackgroundColor(Color.GREEN);
Handler handler=new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
correct++;
OptionA.setBackgroundColor(Color.parseColor("#03A9F4"));
updateQuestions();
}
},1500);
}
else
{
//answer if wrong...we will find the correct answer and make it green
wrong++;
OptionA.setBackgroundColor(Color.RED);
if(OptionB.getText().toString().equals(question.getAnswer()))
{
OptionB.setBackgroundColor(Color.GREEN);
}
else if(OptionC.getText().toString().equals(question.getAnswer()))
{
OptionC.setBackgroundColor(Color.GREEN);
}
else if(OptionD.getText().toString().equals(question.getAnswer()))
{
OptionD.setBackgroundColor(Color.GREEN);
}
//Replace all the colors and update the question
Handler handler=new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
OptionA.setBackgroundColor(Color.parseColor("#FDCC12"));
OptionB.setBackgroundColor(Color.parseColor("#FDCC12"));
OptionC.setBackgroundColor(Color.parseColor("#FDCC12"));
OptionD.setBackgroundColor(Color.parseColor("#FDCC12"));
updateQuestions();
}
},1500);
}
}
});
OptionB.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(OptionB.getText().toString().equals(question.getAnswer()))
{
OptionB.setBackgroundColor(Color.GREEN);
Handler handler=new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
correct++;
OptionB.setBackgroundColor(Color.parseColor("#03A9F4"));
updateQuestions();
}
},1500);
}
else
{
//answer if wrong...we will find the correct answer and make it green
wrong++;
OptionB.setBackgroundColor(Color.RED);
if(OptionA.getText().toString().equals(question.getAnswer()))
{
OptionA.setBackgroundColor(Color.GREEN);
}
else if(OptionC.getText().toString().equals(question.getAnswer()))
{
OptionC.setBackgroundColor(Color.GREEN);
}
else if(OptionD.getText().toString().equals(question.getAnswer()))
{
OptionD.setBackgroundColor(Color.GREEN);
}
//Replace all the colors and update the questions
Handler handler=new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
OptionA.setBackgroundColor(Color.parseColor("#FDCC12"));
OptionB.setBackgroundColor(Color.parseColor("#FDCC12"));
OptionC.setBackgroundColor(Color.parseColor("#FDCC12"));
OptionD.setBackgroundColor(Color.parseColor("#FDCC12"));
updateQuestions();
}
},1500);
}
}
});
OptionC.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(OptionC.getText().toString().equals(question.getAnswer()))
{
OptionC.setBackgroundColor(Color.GREEN);
Handler handler=new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
correct++;
OptionC.setBackgroundColor(Color.parseColor("#03A9F4"));
updateQuestions();
}
},1500);
}
else
{
//answer if wrong...we will find the correct answer and make it green
wrong++;
OptionC.setBackgroundColor(Color.RED);
if(OptionA.getText().toString().equals(question.getAnswer()))
{
OptionA.setBackgroundColor(Color.GREEN);
}
else if(OptionB.getText().toString().equals(question.getAnswer()))
{
OptionB.setBackgroundColor(Color.GREEN);
}
else if(OptionD.getText().toString().equals(question.getAnswer()))
{
OptionD.setBackgroundColor(Color.GREEN);
}
//Replace all the colors and update the questions
Handler handler=new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
OptionA.setBackgroundColor(Color.parseColor("#FDCC12"));
OptionB.setBackgroundColor(Color.parseColor("#FDCC12"));
OptionC.setBackgroundColor(Color.parseColor("#FDCC12"));
OptionD.setBackgroundColor(Color.parseColor("#FDCC12"));
updateQuestions();
}
},1500);
}
}
});
OptionD.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(OptionD.getText().toString().equals(question.getAnswer()))
{
OptionD.setBackgroundColor(Color.GREEN);
Handler handler=new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
correct++;
OptionD.setBackgroundColor(Color.parseColor("#03A9F4"));
updateQuestions();
}
},1500);
}
else
{
//answer if wrong...we will find the correct answer and make it green
wrong++;
OptionD.setBackgroundColor(Color.RED);
if(OptionA.getText().toString().equals(question.getAnswer()))
{
OptionA.setBackgroundColor(Color.GREEN);
}
else if(OptionB.getText().toString().equals(question.getAnswer()))
{
OptionB.setBackgroundColor(Color.GREEN);
}
else if(OptionC.getText().toString().equals(question.getAnswer()))
{
OptionC.setBackgroundColor(Color.GREEN);
}
//Replace all the colors and update the questions
Handler handler=new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
OptionA.setBackgroundColor(Color.parseColor("#FDCC12"));
OptionB.setBackgroundColor(Color.parseColor("#FDCC12"));
OptionC.setBackgroundColor(Color.parseColor("#FDCC12"));
OptionD.setBackgroundColor(Color.parseColor("#FDCC12"));
updateQuestions();
}
},1500);
}
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
}));
}
}
}
我只希望按钮能够显示 Firebase 数据库中的数据并且颜色能够正常工作,因为现在它们在按下时都会变成红色
问题解决了我只需要将 JSON 文件导入我的数据库,而不是从 Firebase 数据库创建它
解决方案
您确定从数据库中检索到的选项具有值吗?由于您没有在从数据库中检索数据的代码中提供更多详细信息,请检查您的选项是否具有以下值:
Log.i("OptionA value: ", question.getOption1());
检查你的 logcat 并告诉我们你看到了什么。您 OptionButtons 的文本似乎为空。所有按钮都变红是正常的,因为答案与空值进行比较。
推荐阅读
- docker - 如何在使用 Gitlab CI runner 拉取 docker:dind 之前在 Dockerhub 上进行身份验证
- java - JavaFX getStylesheet().add(path) 不允许我从 Maven 项目的根目录读取
- python - 如何在 Selenium 中定位标签按钮(Angular 组件)?
- c# - 错误:GetResultCore(布尔值 waitCompletionNotification)
- python - 为给定的数据点创建简单的 networkx 图形布局
- php - 输入类型文件多个jQuery laravel
- javascript - 如何使用 JavaScript 中的输入值进行计算?
- mysql - 字符串以数字开头,查询后返回不同的结果
- python - 使用 Dropbox URL 获取 Dropbox 文件内容
- javascript - 如何使用数组数组查找数组