我正在尝试制作一个 listView 来显示一个问题,其中包含可变数量的复选框。我正在使用扩展 BaseAdapter 的类。当列表需要滚动然后重复行时,我的问题就来了。我知道我的错误在哪里,但我无法得到解决方案。我需要从metod getView中提取部分代码,就像我在问题标题中所做的那样,但我无法完成其余部分,请帮助。
private class AdaptadorListaPreguntas extends BaseAdapter{
ArrayList<String> array;
public AdaptadorListaPreguntas(ArrayList<String> lista){
array=lista;
}
@Override
public int getCount() {
return array.size();
}
@Override
public Object getItem(int position) {
return array.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
HolderListaPreguntas holder;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
String frase = array.get(position);
if (convertView == null)
{
holder = new HolderListaPreguntas();
convertView = inflater.inflate(R.layout.lista_multiple_eleccion, parent, false);
convertView.setTag(holder);
//Count how many checkbox i need
for (int i=0 ; i<frase.length(); i++){
if (frase.charAt(i) == '\n'){
nCheckBox++;
}
}
//Rest one, need for the format of the string
nCheckBox--;
for(int i=0; i<nCheckBox; i++){
CheckBox cb = new CheckBox(getApplicationContext(),null,android.R.attr.checkboxStyle);
cb.setTextColor(Color.BLACK);
cb.setButtonDrawable(R.drawable.checkbox_multiple_eleccion);
//Put checkbox in to layout
holder.layout = (LinearLayout) convertView.findViewById(R.id.checkboxmultiple);
holder.layout.addView(cb);
}
//reset for next loop
nCheckBox = 0;
}
else{
holder = (HolderListaPreguntas) convertView.getTag();
}
holder.tituloPregunta = (TextView) convertView.findViewById(R.id.titulopreguntamultple);
holder.id = position;
holder.layout = (LinearLayout) convertView.findViewById(R.id.checkboxmultiple);
//Get text for each CheckBox
int indice = frase.indexOf("-");
String titulo = frase.substring(frase.indexOf(")")+1, frase.indexOf("-", indice+1));
holder.tituloPregunta.setText(titulo);
//Put all elements of layout in array
ArrayList<View> respuestas = holder.layout.getTouchables();
//Format of the text (only need "question") -Question;/;(id:n)(vof)
int indPriPreInit = frase.indexOf("-", frase.indexOf("-")+1);
int indPriPreFin= frase.indexOf(";/;");
//put text in the checkbox
for (View cb : respuestas){
if(cb instanceof CheckBox){
indPriPreInit = frase.indexOf("-", indPriPreInit);
indPriPreFin = frase.indexOf(";/;", indPriPreFin);
if (indPriPreInit > -1 && indPriPreFin > -1){
String textoCheckBox = (String) frase.subSequence(indPriPreInit, indPriPreFin);
indPriPreInit = indPriPreInit+1;
indPriPreFin = indPriPreFin+1;
//Relleno el texto del checkbox
((CheckBox) cb).setText(textoCheckBox);
}
}
}
return convertView;
}
}