Android recyclerView - create fields dynamically

Asked

Viewed 132 times

0

How do I create fields dynamically in a RecyClerView?

I’m trying to insert a RadioButton I can’t do it manually. All the examples I found, shows that you have to first create an XML layout in order to later create in Adapter. But since the amount of items I’m going to create doesn’t have the right amount, I need to do it dynamically.

I have to create a form where you will present questions and answers. As each question will have N amount of answers, I have no way to create an xml layout with a certain amount of fields, as this can change from question to question.

Follow my code so you can understand better:

public class AdapterPergunta extends RecyclerView.Adapter<AdapterPergunta.MyViewHolder> {
private ArrayAdapter<PerguntaPOJO> adpPergunta;
private ArrayAdapter<RespostaPOJO> adpResposta;
private List<RespostaPOJO> listResposta;
private LayoutInflater mLayoutInflater;
private LinearLayout lnItemAdapterPergunta;
private int mTipoPergunta;
private RecyclerViewOnClickListenerAdapterPergunta mRecyclerViewOnClickListenerAdapterPergunta;
private Context mcontext;
private RadioGroup myRadiogroup;
private RadioButton myRadiobutton;
private PerguntaPOJO perguntaPOJO;
private RespostaDAO respostaDAO;
private RespostaPOJO respostaPOJO;
private int contPergunta;
private boolean alimentou;

public AdapterPergunta(Context context, ArrayAdapter<PerguntaPOJO> adp){
    adpPergunta = adp;
    mcontext = context;
    mLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //myRadiogroup = new RadioGroup(mcontext);
    lnItemAdapterPergunta = new LinearLayout(mcontext);
    //createRadioGroup();
    //lnItemAdapterPergunta.addView(myRadiogroup);
    respostaPOJO = new RespostaPOJO();
    respostaDAO = new RespostaDAO(mcontext);
    perguntaPOJO = new PerguntaPOJO();
    contPergunta=0;
    alimentou = false;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //Chamado quando tem necessidade de criar uma nova View
    View view = mLayoutInflater.inflate(R.layout.item_adapter_pergunta_recyclerview, parent, false);
    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    //vincula os dados do DB com a View
    holder.tv_pergunta_adapter.setText("P" + String.valueOf(position) + "-" + adpPergunta.getItem(position).getDescricao());
    listResposta = respostaDAO.listaRespostaAdapter(mcontext, adpPergunta.getItem(position).getIdpergunta());
    alimentou = true;
    contPergunta = contPergunta + 1;

    Log.d("Position: ", String.valueOf(position));
    Log.d("Pergunta: ", String.valueOf(adpPergunta.getItem(position).getIdpergunta() + "-" +
            adpPergunta.getItem(position).getDescricao()));
    myRadiobutton = new RadioButton(mcontext);
    if (adpPergunta.getItem(position).getTipopergunta() == perguntaPOJO.TIPOMULTIPLAESCOLHA) {
        adpResposta = respostaDAO.preencheLista(mcontext, adpPergunta.getItem(position).getIdpergunta());
        for (int i = 0; i < adpResposta.getCount(); i++){
            if (adpPergunta.getItem(position).getIdpergunta() == adpResposta.getItem(i).getIdpergunta()) {
                respostaPOJO.setDescricao(adpResposta.getItem(i).getDescricao());
                respostaPOJO.setIdresposta(adpResposta.getItem(i).getIdresposta());

                myRadiobutton.setText(respostaPOJO.getDescricao());
                myRadiobutton.setId(respostaPOJO.getIdresposta());
                //myRadiogroup.addView(myRadiobutton);
                //createRadioButton(respostaPOJO);
                Log.d("Resposta: ", respostaPOJO.getIdresposta()+"-"+respostaPOJO.getDescricao());
            }
        }
    }


    try {
        YoYo.with(Techniques.BounceInUp)
                .duration(700)
                .playOn(holder.itemView);
    } catch (Exception e) {

    }
}

@Override
public int getItemCount() {
    //tamanho da lista
    int count = adpPergunta.getCount();
    return count;
}


public void setRecyclerViewOnClickListenerAdapterPergunta(RecyclerViewOnClickListenerAdapterPergunta r) {
    mRecyclerViewOnClickListenerAdapterPergunta = r;
}

public void addAdpterItem(PerguntaPOJO perguntaPOJO, int position) {
    adpPergunta.add(perguntaPOJO);
    notifyItemInserted(position);
}

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView tv_pergunta_adapter;
    public RadioGroup rgItemAdapter;
    public RadioButton rbItemAdapter;
    public LinearLayout lnItemAdapterPergunta;
    //public RadioGroup radioGroup;
    public MyViewHolder(View itemView) {
        super(itemView);

        tv_pergunta_adapter = (TextView)itemView.findViewById(R.id.tv_pegunta_adapter);
        lnItemAdapterPergunta = (LinearLayout)itemView.findViewById(R.id.lnItemAdapterPergunta);

        myRadiogroup = new RadioGroup(mcontext);
        LinearLayout.LayoutParams rgParam = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        myRadiogroup.setOrientation(RadioGroup.VERTICAL);
        lnItemAdapterPergunta.addView(myRadiogroup);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mRecyclerViewOnClickListenerAdapterPergunta != null) {
            mRecyclerViewOnClickListenerAdapterPergunta.onClickListener(v, getLayoutPosition());
        }
    }
}

private void createRadioGroup() {
    try {
        myRadiogroup = new RadioGroup(mcontext);
        LinearLayout.LayoutParams rgParam = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);

        myRadiogroup.setOrientation(RadioGroup.VERTICAL);
        lnItemAdapterPergunta.addView(myRadiogroup, rgParam);
    } catch (Exception e) {
        Log.d("Erro RadioGroup: ", e.toString());
    }
  }
}

  • 1

    Thanks @rubStackOverflow!

  • Once upon a time reply for an equal problem where I used a ExpandableListView to contain the questions and answers.

  • 1

    Ok @ramaral. I actually got to see your response to this kind of implementation. Only since I was trying to implement it in Recyclerview, I kept trying to find a way to do it. But after I saw your answer, I started thinking about a plan B, which is the one I’m starting now. Thank you for the answer. I will implement it for my case. Anything, I inform here about the result. Hug my dear!

  • There must be other ways of implementation but this kind of situation "fits like a glove" in the use of a ExpandableListView: a list(questions) whose items are also a list(answers).

  • Dude, that’s what happened. It fit like a glove. It’s perfect. I’m still implementing all the forms of questions that I created before on a Listview on Expandable to see if everything will behave properly. But I am very happy, because just as the colleague who gave you the solution, I was stuck for weeks and my time was running out to come up with a solution. Congratulations @ramaral. Thank you very much. I will give feedback on the other modes that I will create, but there is no reason to go wrong, because I will only change widget (Edittext, Radiobutton, Imageview, etc.) Thank you very much for the support.

  • @ramaral my dear, I managed to create my form right, as I said above. Only now I had to do something that I could not. I need to go through the children of my Expandablelistview. By clicking on the questions in the form, it automatically advances to the next question, only it has a question that I need to click, and proceed to the next answer of the same question. Not being able to get the exact position of the answers, which are the children. You?

  • I wish I could help, but I can’t understand what your question is. I suggest you ask another question, put in the relevant code and try to explain the problem better.

  • This is the link to my question : http://answall.com/questions/122688/obtainable%C3%A7%C3%A3o-de-childview-em-expandablelistview

Show 3 more comments
No answers

Browser other questions tagged

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