0

I didn't show data in layout, but in logcat the output is shown.I am storing some data in firebase realtime database it's works but this fetched data couldn't show in layout.

ShowQuiz Activity:

public class ShowQuizActivity extends AppCompatActivity implements View.OnClickListener {

    final static int INTERVAL = 1000;
    final static int TIMEOUT = 7000;

    int progressValue = 0;

    List<Question> questionList;
    CountDownTimer mCountDownTimer;

    ProgressBar progressBar;
    Button btnA,btnB,btnC,btnD;
    TextView  txtScore,txtQuestionNum,txtQuestion;

    int score = 0,index = 0,thisQuestion = 0,totalQuestion,correctAnswer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_quiz);

        questionList = new ArrayList<>();

        progressBar = findViewById(R.id.progressBar);

        txtQuestion = findViewById(R.id.question);
        txtQuestionNum = findViewById(R.id.quNum);
        txtScore = findViewById(R.id.score);

        btnA = findViewById(R.id.option1);
        btnB = findViewById(R.id.option2);
        btnC = findViewById(R.id.option3);
        btnD = findViewById(R.id.option4);

        btnA.setOnClickListener(this);
        btnB.setOnClickListener(this);
        btnC.setOnClickListener(this);
        btnD.setOnClickListener(this);

        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference questionRef = rootRef.child("Question");
        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //List<Question> questionList = new ArrayList<>();
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    Question q = ds.getValue(Question.class);
                    questionList.add(q);
                    String answer = q.getAnswer();
                    String option1 = q.getOption1();
                    String option2 = q.getOption2();
                    String option3 = q.getOption3();
                    String option4 = q.getOption3();
                    String question = q.getQuestion();
                    Log.d("=>", answer + "/" + option1 + "/" + option2 + "/" + option3 + "/" + option4 + "/" + question);
                }

                //Do what you need to do with your list
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.d("=>", databaseError.getMessage()); //Don't ignore errors!
            }
        };
        questionRef.addListenerForSingleValueEvent(valueEventListener);
    }

    @Override
    public void onClick(View view) {

        mCountDownTimer.cancel();

        if (index < totalQuestion){
            Button ClickedButton = (Button) view;
            if (ClickedButton.getText().equals(questionList.get(index).getAnswer())){

                score+=10;
                correctAnswer++;
                showQuestion(index++);
            }
            else {
                showQuestion(++index);
            }
            txtScore.setText(String.format("%d",score));
        }

    }

    private void showQuestion(int index) {

        if (index < totalQuestion){
            thisQuestion++;
            txtQuestionNum.setText(String.format("%d/%d",thisQuestion,totalQuestion));
            progressBar.setProgress(0);
            progressValue = 0;

            txtQuestion.setText(questionList.get(index).getQuestion());
            btnA.setText(questionList.get(index).getOption1());
            btnB.setText(questionList.get(index).getOption2());
            btnC.setText(questionList.get(index).getOption3());
            btnD.setText(questionList.get(index).getOption4());

        }
        else {
            Intent intent = new Intent(this,ScoreActivity.class);
            Bundle dataSend = new Bundle();
            dataSend.putInt("SCORE",score);
            dataSend.putInt("TOTAL",totalQuestion);
            dataSend.putInt("CORRECT",correctAnswer);
            intent.putExtras(dataSend);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        totalQuestion = questionList.size();
        mCountDownTimer = new CountDownTimer(TIMEOUT,INTERVAL) {
            @Override
            public void onTick(long l) {
                progressBar.setProgress(progressValue);
                progressValue++;
            }

            @Override
            public void onFinish() {

                mCountDownTimer.cancel();
                showQuestion(++index);
            }
        };
        showQuestion(++index);
    }
}

Question class is here:
public class Question {

    private String answer,option1,option2,option3,option4,question;

    public Question() {

    }

    public Question(String answer, String option1, String option2, String option3, String option4, String question) {
        this.answer = answer;
        this.option1 = option1;
        this.option2 = option2;
        this.option3 = option3;
        this.option4 = option4;
        this.question = question;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    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 getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

}

Logcat output image source: https://i.pinimg.com/originals/48/d4/5d/48d45de210192fa3922c24151e3b21de.png what can i do?please help me.Thanks

Edric
  • 24,639
  • 13
  • 81
  • 91
user10997009
  • 142
  • 8
  • Please note that that's not how you should use `Log.d`- you're supposed to supply a _tag_ as the first argument and the message as the second argument. (This can be useful if you want to search for a particular message made by an activity if you use the activity's name.) – Edric Jun 18 '19 at 06:40
  • Thank u@Edric but can u help me in this problem? – user10997009 Jun 18 '19 at 07:24
  • You are getting the data from Firebase in an asynchronous way, so move all your logic that is related to your data inside the callback, inside the `onDataChange()` method. Check also **[this](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)** out. – Alex Mamo Jun 18 '19 at 09:06
  • @AlexMamo you are very helpful, I follow your comment and saw your asynchronous firebase API video and also try this, But I don't work this and understand this. Can u please rewrite my code please.it's very helpful for me.because i am new in firebase site. Again thank u very much.i wiil be waiting for your solution. – user10997009 Jun 18 '19 at 12:26
  • I cannot code for you. You should make your own attempt given the information in that answer and ask another question if something else comes up. – Alex Mamo Jun 18 '19 at 12:28
  • it's okey,thank u for your suggestion – user10997009 Jun 18 '19 at 12:56

0 Answers0