gpt4 book ai didi

java - 等到 firebase 数据检索数据

转载 作者:行者123 更新时间:2023-11-29 23:47:25 26 4
gpt4 key购买 nike

我创建了测验应用程序,并使用 Firebase 实时数据库为我的 Android 应用程序检索数据。在我的 Activity 中,我检索了我的问题、答案和选择的所有数据。但我等待 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 Activity

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);
}
}

最佳答案

由于您的 Db 结构是您拥有问题编号下的所有详细信息,您只需读取问题编号节点即可加载所有数据。

在调用更新问题之前,只需显示一个进度条,并在获取所有数据后将其隐藏在 onDataChange 中。

我还将监听器从值事件更改为单值事件,以便它从数据库中读取一次数据,否则如果不手动删除,您的监听器将保持附加状态。

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

}
});
}

}

关于java - 等到 firebase 数据检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51197814/

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