Как обновить ссылку на базу данных в Android - PullRequest
0 голосов
/ 16 мая 2018

Код

public class TakingQuiz extends AppCompatActivity {

TextView que;
TextView sco;
TextView qn;
Button opt1;
Button opt2;
Button opt3;
Button opt4;
String RecieversId;

Integer mScore = 0;
Integer mQuestionNumber = 1;
String question;
String option1;
String option2;
String option3;
String option4;
String answer;

FirebaseAuth mAuth;
FirebaseUser currentUser;
DatabaseReference mDatabaseReference;

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

    que = (TextView) findViewById(R.id.txtQuestion);
    opt1 = (Button) findViewById(R.id.button1);
    opt2 = (Button) findViewById(R.id.button2);
    opt3 = (Button) findViewById(R.id.button3);
    opt4 = (Button) findViewById(R.id.button4);
    qn = (TextView) findViewById(R.id.number);
    sco = (TextView) findViewById(R.id.score);
    qn.setText("1");
    sco.setText("Score : 0");

    RecieversId = getIntent().getStringExtra("Recievers_Id");

    mAuth = FirebaseAuth.getInstance();
    currentUser = mAuth.getCurrentUser();
    mDatabaseReference = FirebaseDatabase.getInstance().getReference();

    updateQuestion();

}

private void updateQuestion() {
    mDatabaseReference.child("Users").child(RecieversId).child("Quiz").child("Question1").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            question = dataSnapshot.child("Question").getValue().toString();
            answer = dataSnapshot.child("Answer").getValue().toString();
            option1 = dataSnapshot.child("Option1").getValue().toString();
            option2 = dataSnapshot.child("Option2").getValue().toString();
            option3 = dataSnapshot.child("Option3").getValue().toString();
            option4 = dataSnapshot.child("Option4").getValue().toString();
            que.setText(question);
            opt1.setText(option1);
            opt2.setText(option2);
            opt3.setText(option3);
            opt4.setText(option4);


    opt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mQuestionNumber++;
            qn.setText("Quesion : " + mQuestionNumber);
            if (option1.equals(answer)) {
                opt1.setBackgroundColor(Color.GREEN);
                mScore++;
                sco.setText("Score : " + mScore);
            } else
                opt1.setBackgroundColor(Color.RED);
            sco.setText("Score : " + mScore);
        }
    });
    opt2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mQuestionNumber++;
            qn.setText("Quesion : " + mQuestionNumber);
            if (option2.equals(answer)) {
                opt2.setBackgroundColor(Color.GREEN);
                mScore++;
                sco.setText("Score : " + mScore);
            } else
                opt2.setBackgroundColor(Color.RED);
            sco.setText("Score : " + mScore);
        }
    });
    opt3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mQuestionNumber++;
            qn.setText("Quesion : " + mQuestionNumber);
            if (option3.equals(answer)) {
                opt3.setBackgroundColor(Color.GREEN);
                mScore++;
                sco.setText("Score : " + mScore);
            } else
                opt3.setBackgroundColor(Color.RED);
            sco.setText("Score : " + mScore);
        }
    });
    opt4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mQuestionNumber++;
            qn.setText("Quesion : " + mQuestionNumber);
            if (option4.equals(answer)) {
                opt4.setBackgroundColor(Color.GREEN);
                mScore++;
                sco.setText("Score : " + mScore);
            } else
                opt4.setBackgroundColor(Color.RED);
            sco.setText("Score : " + mScore);

        }
    });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

}

} Номер вопроса продолжает увеличиваться, поэтому мне нужно обновить ссылку на базу данных, чтобы получить следующий вопрос в базе данных ... как мне этого добиться ... код до сих порработает, как ожидалось, но я не уверен, как обновить .......................................................................................................................................................

1 Ответ

0 голосов
/ 16 мая 2018

Чтобы перебрать значения в значениях, которые вы получаете из Firebase, вам нужен итератор DataSnapshot.

Вот пример:

mDatabaseReference.child("Users").child(RecieversId).child("Quiz").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot snapshot: dataSnapshot.getChildren()){
            question = snapshot.child("Question").getValue(String.class);
            option1 = dataSnapshot.child("Option1").getValue(String.class);
            //Do bla bla...
        }
    }
}

С помощью этого итератора snapshot вы можете перебирать свои значения в базе данных Firebase. На Do бла бла .. часть вы можете сделать остальную часть своей работы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...