0
I created a list of questions that I received from an api, so I created these questions dynamically, I didn’t use.xml layout, it was all by programming. Now I would like to know how I can read the components of this listview, such as a checkbox for example, since I created it as a check array and I will not "know" which check the user will select, since n checks can be created.
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;
}
Maybe that’s not the best approach to that scenario, look at this reply
– ramaral
I’ll try to apply it, thank you!
– Vinicius da Mata
I’m in trouble with Carview Error:(7) No Resource Identifier found for attribute 'cardCornerRadius' in package 'test.listaviewteste'
– Vinicius da Mata