Iman Akbar Ibrahim
Iman Akbar Ibrahim

Reputation: 29

Wait until firebase data retrieves data

i create quiz app and i using Firebase Realtime Database for retrieving data for my Android application. In my activity I retrieve all data for my questtion,answer,and choice.. but i waiting 4 - 5 sec until question show up.

I want to show a progress bar as long as the data is not completely retrieved from the database. How do I check if all data is completely retrieved so that I can close the progress bar after the data is loaded?

this is 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();
}

}

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

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

Upvotes: 1

Views: 1407

Answers (1)

Umar Hussain
Umar Hussain

Reputation: 3527

Since your Db structure is that you have all the details under question number you can simply read the question number node and you will have all the data loaded.

before calling update question simply show a progress bar and hide it inside onDataChange after getting all the data.

I also changed the listener from Value event to Single Value event so that it reads data from DB once otherwise your listener will remain attach if not removed manually.

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

            }
        });
    }

}

Upvotes: 1

Related Questions