Quizz App Android Firebase

时间:2017-05-15 17:08:04

标签: android firebase firebase-realtime-database

我正在为Android开发一个quizz应用程序。我在我的数据库中使用Firebase。

这是我的代码:

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.tsiw.geographyquizz.Models.Perguntas;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PlayActivity extends AppCompatActivity {

    private ArrayList<String> ArrayPerguntas = new ArrayList<String>();
    private ArrayList<String> ArrayRespostas = new ArrayList<String>();
    private List<String> ArrayRespergunta = new ArrayList<String>();

    TextView dt,pergunta;
    int acertos, perguntaId;
    ImageButton back;
    String distrito;
    Button resp1, resp2, resp3, resp4;
    int aleatorio;
    String perguntaact="";
    int pont = 0;
    char v1,v2,v3,v4;

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

        Typeface t = Typeface.createFromAsset(getAssets(), "fonts/GalanoBold.otf");
        Typeface tLight = Typeface.createFromAsset(getAssets(), "fonts/GalanoLight.otf");
        Typeface tMedium = Typeface.createFromAsset(getAssets(), "fonts/GalanoMedium.otf");

        dt = (TextView) findViewById(R.id.distrito);
        dt.setTypeface(t);
        pergunta = (TextView) findViewById(R.id.pergunta);
        pergunta.setTypeface(tMedium);
        resp1= (Button) findViewById(R.id.resp1);
        resp1.setBackgroundResource(R.color.caixas);
        resp1.getBackground().setAlpha(69);
        resp1.setTypeface(tMedium);
        resp2= (Button) findViewById(R.id.resp2);
        resp2.setBackgroundResource(R.color.caixas);
        resp2.getBackground().setAlpha(69);
        resp2.setTypeface(tMedium);
        resp3= (Button) findViewById(R.id.resp3);
        resp3.setBackgroundResource(R.color.caixas);
        resp3.getBackground().setAlpha(69);
        resp3.setTypeface(tMedium);
        resp4= (Button) findViewById(R.id.resp4);
        resp4.setBackgroundResource(R.color.caixas);
        resp4.getBackground().setAlpha(69);
        resp4.setTypeface(tMedium);

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if(extras != null) {
            distrito = extras.getString("distrito");
            dt.setText(distrito);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        acertos = 0;
        perguntaId = 0;
        CarregaPergunta();
    }

    public void CarregaPergunta(){
            final DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();

            //buscar filhos de distritos
            DatabaseReference myNewRef2 = myRef.child("distritos").child(distrito);

            final ValueEventListener pergListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

                        //Distrito perguntas = postSnapshot.getValue(Distrito.class);
                        Perguntas p = postSnapshot.getValue(Perguntas.class);

                        //encher a classe Pergunta
                        p.setPergunta(postSnapshot.getKey().toString());

                        //buscar perguntas
                        Log.d("ola", "pergunta: " + p);
                        ArrayPerguntas.add(p.getPergunta());
                    }

                    int min = 0;
                    int max = ArrayPerguntas.size() - 1;

                    Random r = new Random();
                    aleatorio = r.nextInt(max - min + 1) + min;

                    perguntaact = ArrayPerguntas.get(aleatorio).toString();
                    pergunta.setText(ArrayPerguntas.get(aleatorio));

                    for (DataSnapshot d : dataSnapshot.getChildren()) {

                        Perguntas pergun = d.getValue(Perguntas.class);
                        //Log.d("ola", pergun.getPergunta().toString());
                    }
                }

                @Override
                public void onCancelled(DatabaseError error) {
                    // Log.w(TAG, "Erro ao ler o valor", error.toException());
                }
            };

            myNewRef2.addValueEventListener(pergListener);


            DatabaseReference myNewRef3 = myRef.child("distritos").child(distrito);


            final ValueEventListener respListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

                        //Distrito perguntas = postSnapshot.getValue(Distrito.class);
                        Perguntas p = postSnapshot.getValue(Perguntas.class);

                        //adiciona todas as respostas de todas as perguntas de aveiro
                        ArrayRespostas.add(postSnapshot.getValue().toString());

                        //Log.d("ola","arrayrespostas:: "+postSnapshot.getValue().toString());
                    }

                    //obter aleatorias respostas
                    Log.d("ola", ArrayRespostas.get(aleatorio));

                    ArrayRespergunta.add(ArrayRespostas.get(aleatorio));

                    Pattern p = Pattern.compile("\\{([^}]*)\\}");
                    Matcher m = p.matcher(ArrayRespergunta.toString());

                    while (m.find()) {

                        String r = m.group(1);
                        String[] myStrings = r.split(",");

                        //RESPOSTAS
                        String r1 = myStrings[0].substring(0, myStrings[0].length() - 2);
                        v1 = myStrings[0].charAt(myStrings[0].length() - 1);
                        resp1.setText(r1);

                        String r2 = myStrings[1].substring(0, myStrings[1].length() - 2);
                        v2 = myStrings[1].charAt(myStrings[1].length() - 1);
                        resp2.setText(r2);

                        String r3 = myStrings[2].substring(0, myStrings[2].length() - 2);
                        v3 = myStrings[2].charAt(myStrings[2].length() - 1);
                        resp3.setText(r3);

                        String r4 = myStrings[3].substring(0, myStrings[3].length() - 2);
                        v4 = myStrings[3].charAt(myStrings[3].length() - 1);
                        resp4.setText(r4);

                        //clique resposta 1
                        resp1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                if (v1 == '0') {
                                    //errada
                                    pont = 0;
                                    Log.d("ola", "ERRASTE");
                                } else {
                                    //certa
                                    pont = 10;
                                    //ir para outra pergunta
                                    Log.d("ola", "ACERTASTE");
                                    acertos++;
                                }
                            }
                        });
                        //clique resposta 1
                        resp2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                if (v2 == '0') {
                                    //errada
                                    pont = 0;
                                    Log.d("ola", "ERRASTE");
                                } else {
                                    //certa
                                    pont = 10;
                                    //ir para outra pergunta
                                    Log.d("ola", "ACERTASTE");
                                    acertos++;
                                }
                            }
                        });
                        //clique resposta 1
                        resp3.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                if (v3 == '0') {
                                    //errada
                                    pont = 0;
                                    Log.d("ola", "ERRASTE");
                                } else {
                                    //certa
                                    pont = 10;
                                    //ir para outra pergunta
                                    Log.d("ola", "ACERTASTE");
                                    acertos++;
                                }
                            }
                        });
                        //clique resposta 1
                        resp4.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                if (v4 == '0') {
                                    //errada
                                    pont = 0;
                                    Log.d("ola", "ERRASTE");
                                } else {
                                    //certa
                                    pont = 10;
                                    Log.d("ola", "ACERTASTE");
                                    //ir para outra pergunta
                                    acertos++;
                                }
                            }
                        });
                    }
                }

                @Override
                public void onCancelled(DatabaseError error) {
                    // Log.w(TAG, "Erro ao ler o valor", error.toException());
                }
            };
            myNewRef3.addValueEventListener(respListener);

            /*perguntaId++;
            if (perguntaId < 3) {
                CarregaPergunta();
            } else {
            *//*Intent i = new Intent(PlayActivity.this, Leaderboard.class);

            Bundle b = new Bundle();
            b.putInt("acertos", acertos);
            startActivity(i);*//*
            }*/
    }



 }

我的界面如下所示:click to see interface

我的代码工作正常。但点击后我无法更改问题,我点击答案的按钮,没有任何反应,我想换到另一个问题。我希望播放器播放8个问题,然后在回答8个问题之后,它会转到OverActivity并显示结果。 我怎样才能做到这一点?

0 个答案:

没有答案