【问题标题】:Wait until firebase data retrieves data等到firebase数据检索数据
【发布时间】:2018-12-14 07:40:59
【问题描述】:

我创建了测验应用程序,并使用 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);
    }
}

【问题讨论】:

    标签: java android firebase firebase-realtime-database


    【解决方案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) {
    
                }
            });
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 2016-02-16
      • 2019-09-11
      • 1970-01-01
      • 2016-12-29
      相关资源
      最近更新 更多