Note that there are some explanatory texts on larger screens.

plurals
  1. PORefresh of a listview in a CustomArrayAdaptor with checbox
    primarykey
    data
    text
    <p>I'm trying to implement a custom array adaptor that has to visualise a list view with one text and one checkbox. I need to remember the selection and I want that every time one option is chececked all the others become unchecked as a radiobutton.</p> <p>This is my code </p> <pre><code>public class RAnswerAdapter extends ArrayAdapter&lt;RAnswer&gt; { private final ArrayList&lt;RAnswer&gt; list; private final Activity context; public RAnswerAdapter(Activity context, ArrayList&lt;RAnswer&gt; list) { super(context, R.layout.answer_item, list); this.context = context; this.list = list; } static class ViewHolder { protected TextView text; protected CheckBox checkbox; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; if (convertView == null) { LayoutInflater inflator = context.getLayoutInflater(); view = inflator.inflate(R.layout.answer_item_radio, null); final ViewHolder viewHolder = new ViewHolder(); viewHolder.text = (TextView) view.findViewById(R.id.answerName); viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkAnsw); viewHolder.checkbox .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Answer element = (Answer) viewHolder.checkbox.getTag(); for(Answer a: list) //if true if(a.isSelected()) //I set at false a.setSelected(false); //I set the new at true element.setSelected(buttonView.isChecked()); //I refresh the list notifyDataSetChanged(); } }); view.setTag(viewHolder); viewHolder.checkbox.setTag(list.get(position)); } else { view = convertView; ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position)); } ViewHolder holder = (ViewHolder) view.getTag(); holder.text.setText(list.get(position).getAnswer_option()); holder.checkbox.setChecked(list.get(position).isSelected()); return view; } </code></pre> <p>}</p> <p>The code actually works in a strange way... The first 4 element of the list work perfectly and all the others aren't checkable! Why this? Where is the error?</p> <p>Thanks for the help</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload