Firebase应用/未加载问题

时间:2018-02-28 14:46:59

标签: firebase-realtime-database

嘿,我做了一个firebase测验应用程序,但存储在数据库中的问题不会加载到应用程序中。谁能告诉我有什么问题?

这是加载问题的代码:

public class Start extends AppCompatActivity {
    Button btnLigue1, btnChampionsligue;
    FirebaseDatabase database;
    DatabaseReference questions;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        database = FirebaseDatabase.getInstance();
        questions = database.getReference("Questions");
        loadQuestion();
        btnChampionsligue = (Button)findViewById(R.id.btnChampionsligue);
        btnLigue1 = (Button)findViewById(R.id.btnLigue1);
        btnChampionsligue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(Start.this,Playing2.class);
                startActivity(intent1);
                finish();
            }
        });
        btnLigue1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Start.this,Playing.class);
                startActivity(intent);
                finish();
            }
        });
    }
    private void loadQuestion() {
        // Fist, clear List if have old question
        if (Common.questionList.size() > 0)
            Common.questionList.clear();
        questions.orderByChild("Questions").addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for(DataSnapshot postSnapchot : dataSnapshot.getChildren())
                        {
                            Question ques = postSnapchot.getValue(Question.class);
                            Common.questionList.add(ques);
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
        // Random list
        Collections.shuffle(Common.questionList);
    }
}

这是我正在玩的代码:

public class Playing extends AppCompatActivity implements View.OnClickListener{
    private DatabaseReference mDatabase;
    final static long INTERVAL = 1000;
    final static long TIMEOUT = 7000;
    int progressValue = 0;
    CountDownTimer mCountDown;
    int index=0,score=0,thisQuestion=0,totalQuestion,correctAnswer;
    ProgressBar progressBar;
    ImageView question_image;
    Button btnA,btnB,btnC,btnD;
    TextView txtScore,txtQuestionNum,question_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playing);
        mDatabase = FirebaseDatabase.getInstance().getReference("Questions");
        //Views
        txtScore = (TextView)findViewById(R.id.txtScore);
        txtQuestionNum = (TextView)findViewById(R.id.txtTotalQuestion);
        question_text = (TextView)findViewById(R.id.question_text);
        question_image = (ImageView)findViewById(R.id.question_image);
        progressBar = (ProgressBar)findViewById(R.id.progressBar);
        btnA = (Button)findViewById(R.id.btnAnswerA);
        btnB = (Button)findViewById(R.id.btnAnswerB);
        btnC = (Button)findViewById(R.id.btnAnswerC);
        btnD = (Button)findViewById(R.id.btnAnswerD);
        btnA.setOnClickListener(this);
        btnB.setOnClickListener(this);
        btnC.setOnClickListener(this);
        btnD.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        mCountDown.cancel();
        if (index < totalQuestion) // Il reste des questions dans la liste
        {
            Button clickedButton =(Button)view;
            if(clickedButton.getText().equals(Common.questionList.get(index).getCorrectAnswer()))
            {
                score+=10;
                correctAnswer++;
                showQuestion(++index); // Question suivante
            }
            else if (clickedButton.getText()!=(Common.questionList.get(index).getCorrectAnswer()))
            {
                showQuestion(++index);
            }
                else
            {
                // Si mauvaise question    TODOO proposer de regarder une pub pour passer à la question suivante
                Intent intent = new Intent(this,Done.class);
                Bundle dataSend = new Bundle();
                dataSend.putInt("SCORE", score);
                dataSend.putInt("TOTAL", totalQuestion);
                dataSend.putInt("CORRECT", correctAnswer);
                intent.putExtras(dataSend);
                //startActivity(intent);
                finish();
            }
            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;
            if(Common.questionList.get(index).getIsImageQuestion().equals("true"))
            {
                // Si il y a une image
                Picasso.with(getBaseContext())
                        .load(Common.questionList.get(index).getQuestion())
                        .into(question_image);
                question_image.setVisibility(View.VISIBLE);
                question_text.setVisibility(View.INVISIBLE);
            }
            else
            {
                question_text.setText(Common.questionList.get(index).getQuestion());

                // Si c'est une question texte : masquer l'image
                question_image.setVisibility(View.INVISIBLE);
                question_text.setVisibility(View.VISIBLE);
            }
            btnA.setText(Common.questionList.get(index).getAnswerA());
            btnB.setText(Common.questionList.get(index).getAnswerB());
            btnC.setText(Common.questionList.get(index).getAnswerC());
            btnD.setText(Common.questionList.get(index).getAnswerD());
            mCountDown.start(); // Start timer
        }
        else
        {
            // Si c'est la dernière question
            Intent intent = new Intent(this,Done.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 = Common.questionList.size();

        mCountDown = new CountDownTimer(TIMEOUT,INTERVAL) {
            @Override
            public void onTick(long minisec) {
                progressBar.setProgress(progressValue);
                progressValue++;
            }
            @Override
            public void onFinish() {
                mCountDown.cancel();
                showQuestion(++index);
            }
        };
        showQuestion(index);
    }
}

0 个答案:

没有答案