Receiving RadioButton value in RecyclerView

0

Good evening.

I would like your help on a problem I'm having, I'm developing an app for a college job, in case I'm going to have an activity that will have some sort of research, or quiz, whatever you want to call it.

The question, and the answers are stored in the firebase, I set everything up on a screen, where the user can select the options for each question. Then there is my question, what I want and the next, the user should select all the answers, and at the end click the button, which will redirect to another activity that will show the result.

This is the problem, I am using a recyclerview to do the list of questions and answers, all in a single activity, how do I store user responses, then send the result to another activity?

xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/fundo_degrade"
    android:fitsSystemWindows="true"
    tools:context="br.com.cifrasemusica.cifrasmusica_teoriamusical.activity.ExercicioActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <include
            android:id="@+id/Toolbar_Exercicio"
            layout="@layout/toolbar">
        </include>

    </android.support.design.widget.AppBarLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior = "@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/RecyclerView_Exercicio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>

    </ScrollView>

    <Button
        android:id="@+id/Button_enviarExercicio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/fundo_botao"
        android:text="Confirmar"
        android:layout_margin="5dp"
        android:layout_gravity="bottom" />

</android.support.design.widget.CoordinatorLayout>

activity

public class ExercicioActivity extends AppCompatActivity {

    // Variaveis globais
    private Toolbar toolbar;
    private String key;
    private String name;
    private String description;
    private ArrayList<Questao> mContent;

    // Firebase
    private FirebaseFirestore firestore;
    private Query query;

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

        // Recuperando os dados passados
        Bundle extra = getIntent().getExtras();
        if (extra != null) {
            this.key = extra.getString("key");
            this.name = extra.getString("name");
            this.description = extra.getString("description");

        }

        // Instanciando os objetos
        firestore = ConexaoFirebase.getFirestore();
        mContent = new ArrayList<>();

        // Configurando a toolbar
        toolbar = findViewById(R.id.Toolbar_Exercicio);
        toolbar.setTitle(this.name);
        toolbar.setTitleTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        setSupportActionBar(toolbar);

        // Buscando conteudo no BD
        DocumentReference documentReference = firestore.collection("exercicios/").document(this.key);
        query = documentReference.collection("questoes/").orderBy("question");
        query.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                mContent.clear();
                //Verificando se existem erros
                if (e != null) {
                    Log.w("Error", "Erro ao ler as questões", e);
                    return;
                }

                // Lendo os dados e salvando no array
                for (DocumentSnapshot doc: documentSnapshots) {
                    if (doc != null) {
                        Questao questao = doc.toObject(Questao.class);
                        questao.setKey(doc.getId());
                        mContent.add(questao);
                    }
                }
                initRecyclerView();
            }
        });
    }

    private void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        RecyclerView recyclerView = findViewById(R.id.RecyclerView_Exercicio);
        recyclerView.setLayoutManager(layoutManager);
        ExercicioAdapter adapter = new ExercicioAdapter(this, mContent);
        recyclerView.setAdapter(adapter);
    }
}

adapter

public class ExercicioAdapter extends RecyclerView.Adapter<ExercicioAdapter.ViewHolder>{

    private static final String TAG = "RecyclerViewAdapter";

    // Variaveis globais
    private ArrayList<Questao> mContent = new ArrayList<>();
    private Context mContext;

    public ExercicioAdapter(Context mContext, ArrayList<Questao> mContent) {
        this.mContent = mContent;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista_exercicios, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        holder.question.setText(mContent.get(position).getQuestion());
        holder.op1.setText(mContent.get(position).getOp1());
        holder.op2.setText(mContent.get(position).getOp2());

    }

    @Override
    public int getItemCount() {
        return mContent.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView question;
        RadioButton op1;
        RadioButton op2;
        Button botao_exercicio;

        public ViewHolder(View itemView) {
            super(itemView);
            this.question = itemView.findViewById(R.id.TextView_titulo_exercicio);
            this.op1 = itemView.findViewById(R.id.RadioButton_op1_exercicio);
            this.op2 = itemView.findViewById(R.id.RadioButton_op2_exercicio);
            this.botao_exercicio = itemView.findViewById(R.id.Button_enviarExercicio);
        }
    }
}
    
asked by anonymous 16.05.2018 / 06:13

1 answer

0

I had a similar problem, but I did not find anything about it. So to solve, I decided to have the user answer one question at a time, I kind of sorted the questions by increasing numbers in the database, there I carried the first question, then when the user answered she would play the answer in db and add +1 in a "int" that I created to be the variable of the questions and loaded the next question (using the same screen just doing another query in db), I would do this until the question variable does not exist in the database.

It's kind of complicated to understand, but this was the base I used, so I started thinking about it, good luck.

    
17.05.2018 / 19:52