1

I store some data in firebase realtime database and create question class.i also create a commonmodel class. Here is the show quiz activity:

public class ShowQuizActivity extends AppCompatActivity implements View.OnClickListener {

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

    CountDownTimer mCountDown;

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

    FirebaseDatabase database;
    DatabaseReference questions;

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


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

        database = FirebaseDatabase.getInstance();
        questions = database.getReference("Question");

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

        progressBar = findViewById(R.id.progressBar);

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

        loadQuestion(CommonModel.question);


    }

    private void loadQuestion(List<Question> question) {

        if (CommonModel.question.size() > 0)
            CommonModel.question.clear();



    }

here is my CommonModel class:

import java.util.ArrayList;
import java.util.List;

public class CommonModel {

    public static SignInUpModel currentUser;
    public static List<Question> question = new ArrayList<>();

}

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

Database structure:

enter image description here

I can't show this data in my app. What can i do?I

Zoe
  • 27,060
  • 21
  • 118
  • 148
user10997009
  • 142
  • 8

1 Answers1

1

According to your comment, to get all questions, please use the following lines of code:

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(TAG, answer + "/" + option1 + "/" + option2 + "/" + option3 + "/" + option4 + "/" + question);
        }

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

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
questionRef.addListenerForSingleValueEvent(valueEventListener);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • You cannot do that since every object within your `Question` node is a `question` object and **not** a `CommonModel`. If you need a list of `Question` objects, when iterating, simply add each `question` object to a list of `Question` objects like in my updated answer. – Alex Mamo Jun 17 '19 at 09:26
  • i fetch data from firebase but it's don't show in layout.thank u – user10997009 Jun 18 '19 at 06:34
  • "It is not working" doesn't help me understand what the problem is. Have you tried my solution above? What is the behaviour? – Alex Mamo Jun 18 '19 at 08:00
  • i am tring your solution,it's works but i fetch this data but it's not showing my layout.please check this link.this is update problem https://stackoverflow.com/questions/56642906/i-didnt-show-data-in-layout – user10997009 Jun 18 '19 at 09:00
  • Regarding the oher question, I'll take a look and if I'll know the answer, I'll write it to you. – Alex Mamo Jun 18 '19 at 09:02