Go through all the rows of a custom listview and get the values of their respective Textview

0

I have a custom list which is loaded with radiobuttons and textview, by selecting a radiobutton (Yes or No) the textview changes to a value, at the end of the list I have a button that when pressed will go through row by row the listview, getting the values and printing them with a toast

While the elements are visible they can be captured with the last visible element, but in my case I have more than 30 rows and these can vary. I would like to capture all the values of the textview. How could I do it?

Try it in the following way, but it does not work.

                for (int i=1; i <= lista.getCount();i++){
                View view = lista.getChildAt(i);
                TextView x = (TextView) view.findViewById(R.id.lbl_respuesta);
                Toast.makeText(getApplicationContext(),String.valueOf(x),Toast.LENGTH_SHORT).show();
            }

Some other way of doing it. I would appreciate your help, thank you very much.

My Listview extends an ArrayAdapter and this is your code

public class Adapter extends ArrayAdapter<Preguntas> {
private Context context;
private ArrayList<Preguntas> datos;

public Adapter(Context context, ArrayList datos) {
    super(context, R.layout.pregunta_respuesta, datos);
    this.context = context;
    this.datos = datos;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View item = convertView;
    final PreguntasHolder holder;
    Preguntas preguntas = datos.get(position);

    if (item == null) {
        item = LayoutInflater.from(context).inflate(R.layout.pregunta_respuesta, null);

        holder = new PreguntasHolder();
        holder.idPregunta = (TextView) item.findViewById(R.id.lbl_idPregunta);
        holder.pregunta = (TextView) item.findViewById(R.id.lbl_pregunta);
        holder.radiogroup = (RadioGroup) item.findViewById(R.id.radioGroup);
        holder.lbl_respuesta = (TextView) item.findViewById(R.id.lbl_respuesta);

        final RadioButton[] rb = new RadioButton[2];
        for (int i=0; i<2;i++){
            rb[i] = new RadioButton(context);
            rb[i].setId(i);

            RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(0,
                    RadioGroup.LayoutParams.WRAP_CONTENT);
            params.weight = 1.0f;
            params.setMargins(5,0,5,10);
            holder.radiogroup.addView(rb[i],params);
        }

        holder.radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbtSi:
                        Toast.makeText(getContext(), "si", Toast.LENGTH_SHORT).show();
                            holder.lbl_respuesta.setText("1");
                        break;
                    case R.id.rbtNo:
                        Toast.makeText(getContext(), "NO", Toast.LENGTH_SHORT).show();
                        holder.lbl_respuesta.setText("0");
                        break;
                    default:
                        break;
                }
            }
        });

        item.setTag(holder);
    }else {

        holder = (PreguntasHolder) item.getTag();
    }

    holder.idPregunta.setText(String.valueOf(preguntas.getIdPregunta()));
    holder.pregunta.setText(String.valueOf(preguntas.getPregunta()));
    return item;
}

public class PreguntasHolder {
    public TextView idPregunta, pregunta,lbl_respuesta;
    public RadioGroup radiogroup;
}

@Override
public int getViewTypeCount() {
    return datos.size();
}

@Override
public int getItemViewType(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}
}
    
asked by Max CM 15.03.2017 в 12:59
source

1 answer

0

And can not you save the value of the radioButton (yes or no) as boolean in the Questions class? And then, instead of going through your ListView, you go through the ArrayList data of your ArrayAdapter. You should only update the value in the RadioGroup listener

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View item = convertView;
    final PreguntasHolder holder;
    Preguntas preguntas = datos.get(position);

    [...]

    holder.radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rbtSi:
                    Toast.makeText(getContext(), "si", Toast.LENGTH_SHORT).show();
                    holder.lbl_respuesta.setText("1");
                    datos.get(position).setRadioGroupValue(true);
                    break;
                case R.id.rbtNo:
                    Toast.makeText(getContext(), "NO", Toast.LENGTH_SHORT).show();
                    holder.lbl_respuesta.setText("0");
                    datos.get(position).setRadioGroupValue(false);
                    break;
                default:
                    break;
            }
        }
    });

    [...]

}
    
answered by 16.03.2017 / 14:05
source