0

Eu criei uma lista de perguntas que eu recebi de uma api, portanto, eu criei essas perguntas dinamicamente, não usei layout.xml, foi tudo programando. Agora eu gostaria de saber como eu posso ler os componentes dessa listView, como uma checkbox por exemplo, já que eu criei ela como um vetor de checks e eu não "saberei" qual check o usuário vai selecionar, uma vez que podem ser criadas n checks.

public class QuestionAdapter extends BaseAdapter{
private List<Pergunta> listaPergunta;
private Context context;
private RadioButton radio[];
private CheckBox check[];
private int qtdeQuestoes,id;
private FrameLayout frameLayout;
int states[][] = {{android.R.attr.state_checkable}, {}};
int colors[] = {R.color.black,R.color.black};
private EditText editText;
private Button button;


public QuestionAdapter(List<Pergunta> listaPergunta, Context context) {
    this.listaPergunta=listaPergunta;
    this.context=context;
}

@Override
public int getCount() {
    return listaPergunta.size();
}

@Override
public Object getItem(int position) {
    return listaPergunta.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, final View convertView, final ViewGroup parent) {
    View v = View.inflate(context, R.layout.question_item, null);

    frameLayout = (FrameLayout) v.findViewById(R.id.frameLayoutQuestion);

    if (listaPergunta.get(position).getTipoPergunta() == 2) {
        id = listaPergunta.get(position).getIdPergunta();
        qtdeQuestoes = listaPergunta.get(position).getQtdeOpcoes();
        radio = new RadioButton[qtdeQuestoes];
        frameLayout.addView(
                createRadioButton(id + "." + listaPergunta.get(position).getTxtPergunta(), listaPergunta.get(position).getOpcoes(), radio));

        radio[0].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                radio[1].setChecked(false);
            }
        });
        radio[1].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                radio[0].setChecked(false);
            }
        });

    } else if (listaPergunta.get(position).getTipoPergunta() == 1) {
        id = listaPergunta.get(position).getIdPergunta();
        qtdeQuestoes = listaPergunta.get(position).getQtdeOpcoes();
        check = new CheckBox[qtdeQuestoes];
        frameLayout.addView(
                createCheck(id + "." + listaPergunta.get(position).getTxtPergunta(), listaPergunta.get(position).getOpcoes(), check));
    }
    return v;
}

public LinearLayout createRadioButton(String question, String[] txt, final RadioButton[] radio){

    int cntradio=0, cnttxt=0;
    LinearLayout linearLayout= new LinearLayout(context);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    ViewGroup.LayoutParams lpView = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    ViewGroup.LayoutParams lpViewRadio =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    TextView tv= new TextView(context);
    tv.setTextColor(Color.BLACK);
    tv.setText(question);
    tv.setLayoutParams(lpView);
    linearLayout.addView(tv);

    while (cntradio < radio.length) {

        radio[cntradio]= new RadioButton(context);
        radio[cntradio].setText(txt[cnttxt]);
        radio[cntradio].setLayoutParams(lpViewRadio);
        radio[cntradio].setTextColor(Color.BLACK);

        linearLayout.addView(radio[cntradio]);
        CompoundButtonCompat.setButtonTintList(radio[cntradio],new ColorStateList(states,colors));
        cntradio = cntradio + 1;
        cnttxt = cnttxt + 1;
    }


    return linearLayout;
}

public LinearLayout createCheck(String question, String[] txt, CheckBox[] check) {

    int cntcheck = 0, cnttxtcheck = 0;
    LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    ViewGroup.LayoutParams lpView = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    ViewGroup.LayoutParams lpViewRadio =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    TextView tv = new TextView(context);
    tv.setTextColor(Color.BLACK);
    tv.setText(question);
    tv.setLayoutParams(lpView);
    linearLayout.addView(tv);



    while (cntcheck < check.length) {

        check[cntcheck] = new CheckBox(context);
        check[cntcheck].setText(txt[cnttxtcheck]);
        check[cntcheck].setLayoutParams(lpViewRadio);
        check[cntcheck].setTextColor(Color.BLACK);
        CompoundButtonCompat.setButtonTintList(check[cntcheck], new ColorStateList(states, colors));
        linearLayout.addView(check[cntcheck]);

        cntcheck++;
        cnttxtcheck++;

    }
    return linearLayout;

}

Esse é o print da tela

3
  • Talvez essa não seja a melhor abordagem para esse cenário, veja esta resposta
    – ramaral
    13/12/2016 às 14:07
  • 1
    Vou tentar aplica-la, grato! 13/12/2016 às 14:31
  • Estou dentro problema com o CarView Error:(7) No resource identifier found for attribute 'cardCornerRadius' in package 'teste.listaviewteste' 13/12/2016 às 14:58

0

Você deve fazer log-in para responder a esta pergunta.

Pesquise outras perguntas com a tag .