0

我正在尝试制作一个 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;
    }

}
4

2 回答 2

0
public class AdaptadorListaPreguntas extends BaseAdapter implements CompoundButton.OnCheckedChangeListener {

    private final ArrayList<String> array;
    private final List<Set<Integer>> checked;

    public AdaptadorListaPreguntas(ArrayList<String> lista){
        array=lista;
        checked = new ArrayList<Set<Integer>>(lista.size());
    }
    @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);
        int checkboxCount = frase.split("\n").length-1;
        if (convertView == null) {
            holder = new HolderListaPreguntas();
            convertView = inflater.inflate(R.layout.lista_multiple_eleccion, parent, false);
            holder.layout = (LinearLayout) convertView.findViewById(R.id.checkboxmultiple);
            convertView.setTag(holder);
        }
        else{
            holder = (HolderListaPreguntas) convertView.getTag();
        }


        Set<Integer> current;
        try {
          current = checked.get(position);
        } catch(NullPointerException e) {
            current = new HashSet<Integer>(checkboxCount);
            checked.set(position, current);
        }

        List<CheckBox> respuestas = new ArrayList<CheckBox>(checkboxCount);
        holder.layout.removeAllViews();

        for(int i=0; i < checkboxCount; i++) {
            CheckBox cb = new CheckBox(getApplicationContext(),null,android.R.attr.checkboxStyle);
            cb.setTextColor(Color.BLACK);
            cb.setButtonDrawable(R.drawable.checkbox_multiple_eleccion);
            respuestas.add(cb);
            cb.setTag(position);
            if (current.contains(position))
                cb.setChecked(true);
            holder.layout.addView(cb);
        }

        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);

        //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 (CheckBox cb : respuestas){
            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
                cb.setText(textoCheckBox);
            }
        }

        return convertView;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int position = (Integer)buttonView.getTag();
        ViewGroup vg = (ViewGroup)buttonView.getParent();
        List<View> touchables = vg.getTouchables();
        int index = touchables.indexOf(buttonView);
        if (isChecked) {
            checked.get(position).add(index);
        } else {
            checked.get(position).remove(index);
        }
    }
}
于 2013-09-20T05:40:19.387 回答
0

最后我决定使用 scrollView 而不是 listView,原因是 listView 使用了我不想使用的回收系统,这是问题所在!

我在以下位置找到了有关 ListView 如何工作的更多信息:链接

谢谢大家的帮助。

于 2013-09-24T11:34:37.853 回答