gpt4 book ai didi

java - 按钮上的 setText 在 TextView 上工作时不起作用

转载 作者:行者123 更新时间:2023-11-30 05:08:18 25 4
gpt4 key购买 nike

这是我的 Firebase 数据库结构: enter image description here

我是 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 Activity 类:

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 数据库创建它

最佳答案

不知道这个问题,但您可以简单地获取字符串中的值,而不是使用字符串变量来设置文本。像那样:

String A, B, C, D;

A = question.getOption1();
B = question.getOption2();
C = question.getOption3();
D = question.getOption4();

OptionA.setText(A);
OptionB.setText(B);
OptionC.setText(C);
OptionD.setText(D);

只是一个建议它会为你工作。

关于java - 按钮上的 setText 在 TextView 上工作时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54163452/

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