Receive Radiobutton value in Recyclerview

Asked

Viewed 67 times

0

good night.

I would like your help in a problem that I’m having, I’m developing an app for a college job, in case I’m going to have an Activity that will have a kind of research, or quiz, as you want to call.

The question, and the answers are stored in the firebase, I list everything on a screen, where the user will be able to select the options for each question. There is my question, what I want and the following, 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.

That’s the problem, I’m using a recyclerview to list the questions and answers, all in a single Activity, as I do to store the user’s answers, to 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);
        }
    }
}

1 answer

0

I had a similar problem, only I couldn’t find anything about it. So to solve I decided to make the user answer one question at a time, sort the questions by increasing numbers in the database, then load the first question, then when the user answered it played the answer in db and added +1 in an "int" I created to be the variable of the questions and loaded the next question (using the same screen only doing another query in db), was doing this until the variable of the questions does not exist in the database.

It’s kind of hard to understand, but this is the base I used, so you can start thinking from there, good luck.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.