我创建测验应用程序,并使用Firebase实时数据库为我的Android应用程序检索数据。在我的活动中,我检索所有有关我的问题,答案和选择的数据。但是我等待4到5秒钟,直到出现问题为止。
只要数据没有从数据库中完全检索出来,我想显示一个进度条。如何检查是否已完全检索所有数据,以便在加载数据后关闭进度栏?
这是mainActivity.class
public class MainActivity extends AppCompatActivity {
private TextView mScoreView;
private TextView mQuestion;
private Button mButtonChoice1, mButtonChoice2, mButtonChoice3, mButtonChoice4;
public int mScore = 0;
private int mQuestionNumber = 0;
private String mAnswer;
public static String alertTitle;
private Firebase mQuestionRef, mchoice1Ref, mChoice2Ref, mChoice3Ref, mChoice4Ref, mAnswerRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mScoreView = (TextView)findViewById(R.id.score);
mQuestion = (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();
//button1
mButtonChoice1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mButtonChoice1.getText().equals(mAnswer)){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
alertTitle = "Correct!";
corectanswer();
}else {
updateQuestion();
wrong();
alertTitle = "Wrong!";
wronganswer();
}
}
});
//button1
//button2
mButtonChoice2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mButtonChoice2.getText().equals(mAnswer)){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
alertTitle = "Correct!";
corectanswer();
}else {
updateQuestion();
wrong();
alertTitle = "Wrong!";
wronganswer();
}
}
});
//button2
//button3
mButtonChoice3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mButtonChoice3.getText().equals(mAnswer)){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
alertTitle = "Correct!";
corectanswer();
}else {
updateQuestion();
wrong();
alertTitle = "Wrong!";
wronganswer();
}
}
});
//button3
//button4
mButtonChoice4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mButtonChoice4.getText().equals(mAnswer)){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
alertTitle = "Correct!";
corectanswer();
}else {
updateQuestion();
wrong();
alertTitle = "Wrong!";
wronganswer();
}
}
});
//button4
}
private void wrong(){
//Toast.makeText(MainActivity.this, "Wrong", Toast.LENGTH_SHORT).show();
}
private void updateScore(int score){
mScoreView.setText("" + mScore);
}
private void updateQuestion() {
mQuestionRef = new Firebase("https://maths-quiz-d33d7.firebaseio.com/" + mQuestionNumber + "/question");
mQuestionRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question = dataSnapshot.getValue(String.class);
mQuestion.setText(question);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
mchoice1Ref = new Firebase("https://maths-quiz-d33d7.firebaseio.com/" + mQuestionNumber + "/choice1");
mchoice1Ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice = dataSnapshot.getValue(String.class);
mButtonChoice1.setText(choice);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
mChoice2Ref = new Firebase("https://maths-quiz-d33d7.firebaseio.com/" + mQuestionNumber + "/choice2");
mChoice2Ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice = dataSnapshot.getValue(String.class);
mButtonChoice2.setText(choice);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
mChoice3Ref = new Firebase("https://maths-quiz-d33d7.firebaseio.com/" + mQuestionNumber + "/choice3");
mChoice3Ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice = dataSnapshot.getValue(String.class);
mButtonChoice3.setText(choice);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
mChoice4Ref = new Firebase("https://maths-quiz-d33d7.firebaseio.com/" + mQuestionNumber + "/choice4");
mChoice4Ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String choice = dataSnapshot.getValue(String.class);
mButtonChoice4.setText(choice);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
mAnswerRef = new Firebase("https://maths-quiz-d33d7.firebaseio.com/" + mQuestionNumber + "/answer");
mAnswerRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mAnswer = dataSnapshot.getValue(String.class);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
if(mQuestionNumber == 4){
corectanswer();
wronganswer();
Intent intent = new Intent(MainActivity.this,FinishActivity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", mScore);
startActivity(intent);
}else {
mQuestionNumber++;
}
}
public void corectanswer(){
new AlertDialog.Builder(MainActivity.this)
.setTitle(alertTitle)
.setMessage(mAnswer)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.cancel();
}
}).show();
}
public void wronganswer(){
new AlertDialog.Builder(MainActivity.this)
.setTitle(alertTitle)
.setMessage(mAnswer)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.cancel();
}
}).show();
}
}
和这个startactivity.class
import com.google.firebase.database.FirebaseDatabase;
public class StartActivity extends AppCompatActivity {
private Button mulai;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
TextView label = (TextView)findViewById(R.id.quizmudah);
label.setText("proooo");
mulai = (Button)findViewById(R.id.mulai);
mulai.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new `Intent(getApplicationContext(),MainActivity.class);`
startActivity(intent);
}
});
}
}
这是mathQuiz.class活动
package com.belajardatabase.www.bikinquiz3;
import android.app.Application;
import com.firebase.client.Firebase;
/**
* Created by Iman on 7/3/2018.
*/
public class MathsQuiz extends Application {
@Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
}
}
答案 0 :(得分:1)
由于您的Db结构是您拥有问题编号下的所有详细信息,因此您只需读取问题编号节点,就可以加载所有数据。
在调用更新问题之前,只需显示进度条,并在获取所有数据后将其隐藏在onDataChange
中即可。
我还将侦听器从Value事件更改为Single Value事件,以便它一次从DB读取数据,否则,如果不手动删除,您的侦听器将保持连接状态。
private void updateQuestion() {
if(mQuestionNumber == 4){
corectanswer();
wronganswer();
Intent intent = new Intent(MainActivity.this,FinishActivity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", mScore);
startActivity(intent);
}else {
mQuestionNumber++;
mQuestionRef = new Firebase("https://maths-quiz-d33d7.firebaseio.com/" + mQuestionNumber );
mQuestionRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question = dataSnapshot.child("question").getValue(String.class);
mQuestion.setText(question);
String choice1 = dataSnapshot.child("choice1").getValue(String.class);
mButtonChoice1.setText(choice1);
String choice2 = dataSnapshot.child("choice2").getValue(String.class);
mButtonChoice2.setText(choice2);
String choice3 = dataSnapshot.child("choice3").getValue(String.class);
mButtonChoice3.setText(choice3);
String choice4 = dataSnapshot.child("choice4").getValue(String.class);
mButtonChoice4.setText(choice4);
mAnswer = dataSnapshot.getValue(String.class);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
}