4

I struggle with a common problem however I can't see the solution to it. What I'm trying to achieve is to display the list of questions (they come from parsed JSON) and radioGroup under each. enter image description here

I've manage to get questions list displayed. Now when I check the buttons and scroll down the list the upper buttons are loosing it state. I have followed and read about getView() and tryed different ways:

stackoverflow.com/...how-to-handle-oncheckedchangelistener-for-a-radiogroup-in-a-custom-listview-adap

and here:

stackoverflow.com...android-how-to-make-radiogroup-work-correctly-in-a-listview

I'm using the HashMap in my ArrayAdapter. I was trying to 'convert' these to array of objects but without success. I have no idea how to make each of the buttons to keep it's state.

Can you point out what I'm doing wrong or what I'm missing???

I've included the code for arrayAdater and a mockup of my xml visual layout.

public class QuestionListAdapter extends ArrayAdapter<HashMap<String, Object>> {
    PlayerData plData = new PlayerData();
    public int current = NONE;
    public static final int NONE = 1000;
    public static String id;
    private Activity _activity;
    private ArrayList<HashMap<String, Object>> _questionsList;
    private HashMap<String, Object> answers;

    public QuestionListAdapter(Activity activity, int resource,
            ArrayList<HashMap<String, Object>> questionsList) {
        super(activity, resource, questionsList);
        _activity = activity;
        _questionsList = questionsList;
    }

    public static final class ViewHolder {
        TextView question;
        RadioGroup radioG;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder = null;

        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater) _activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.question_right_fragment,
                    null);
            holder = new ViewHolder();
            holder.question = (TextView) view.findViewById(R.id.question);
            holder.radioG = (RadioGroup) view.findViewById(R.id.radio0);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        final HashMap<String, Object> pData = _questionsList.get(position);
        if (pData != null) {
            holder.question.setText((String) pData.get("questionText"));

            // holder.radioG.clearCheck();
            holder.radioG
                    .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(RadioGroup group,
                                int checkedId) {
                            Integer pos = (Integer) group.getTag();

                            switch (checkedId) {

                            case R.id.radio1:
                                plData.setQ1((byte) 1);
                                break;

                            case R.id.radio2:
                                plData.setQ2((byte) 2);
                                break;

                            case R.id.radio3:
                                plData.setQ3((byte) 3);
                                break;

                            case R.id.radio4:
                                plData.setQ4((byte) 4);
                                break;

                            case R.id.radio5:
                                plData.setQ5((byte) 5);
                                break;

                            case R.id.radio6:
                                plData.setQ6((byte) 6);
                                break;

                            case R.id.radio7:
                                plData.setQ1((byte) 7);
                                break;

                            default:

                            }
                            Log.d("IN PLDATA:", plData.toString());
                            answers.put("id", plData);

                        }
                    });
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.radioG.setTag(new Integer(position));
        if (_questionsList.get(position).get(
                holder.radioG.getCheckedRadioButtonId()) != null) {
            RadioButton r = (RadioButton) holder.radioG
                    .getChildAt((Integer) _questionsList.get(position).get(
                            holder.radioG.getCheckedRadioButtonId()));
            r.setChecked(true);
        } else {
            holder.radioG.clearCheck();
        }

        return view;
    }

}
Community
  • 1
  • 1
Jakubee
  • 634
  • 1
  • 9
  • 30
  • I had a similar problem with edit texts losing content when scrolling the list view. Couldn't find a solution and ended up inflating the views in a loop and adding them to a ScrollView. – developeralways Nov 20 '13 at 18:06

1 Answers1

4

For future reference. Use the SparseIntArray. In my case I called it checked.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.question_right_fragment, null);
        holder = new ViewHolder();
        holder.question = (TextView) convertView.findViewById(R.id.question);
        holder.radioG = (RadioGroup) convertView.findViewById(R.id.radio0);

        convertView.setTag(holder);


    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    final HashMap<String, Object> pData = _questionsList.get(position);
    if (pData != null){
        holder.question.setText((String)pData.get("questionText"));
    }else{
        holder.question.setText("");    
    }

    holder.radioG.setOnCheckedChangeListener(null);
    holder.radioG.clearCheck();

    if(checked.indexOfKey(position)>-1){
        holder.radioG.check(checked.get(position));
    }else{
        holder.radioG.clearCheck();
    }
    holder.radioG.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId>-1){
                checked.put(position, checkedId);
            }else{
                if(checked.indexOfKey(position)>-1)
                    checked.removeAt(checked.indexOfKey(position));
            }
        }
    });

    return convertView;
}
Jakubee
  • 634
  • 1
  • 9
  • 30
  • Hi @SIVAKUMAR.J checked is the SparseIntArray. you can define it in your onCreate call back and use it in getView() of your custom adapter. – MohanRaj Oct 13 '14 at 12:29