1

I have a ListView of choices, where I check/choose the items to answer a question and place the selected choices in a custom array class(Options) and pass it to the Confirmation activity. For example: I have 4 choices in my ListView

Question: What is your favorite color?

[] Red
[] Green
[] Blue
[] Yellow

(Everytime a checkbox is checked, I increment checkCounter which is a global variable. Everytime a checkbox is unchecked, checkboxCounter is decremented.)

Sample scenario: I check choices Blue and Yellow and submit it to the next activity(Confirmation), the checkboxCounter is now 2 but I want to change my answer so I go back to Answer activity but in my Log, checkboxCounter is +1 or incremented to 1. The final checkboxCounter is now 3, I don't know where I went wrong or if it reused the global variable checkboxCounter.

Here's my code:

private class OptionAdapter extends ArrayAdapter<Option>  {
    Context context; 
    int layoutResourceId;    
    OptionHolder holder;
    Option data[] = null;

    public OptionAdapter(Context context, int layoutResourceId, Option[] data) {
        super(context, layoutResourceId, data);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.data = data;
    }

    private class OptionHolder {
        CheckBox optionCheckBox;
        TextView  optionTextView;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        holder = null;
        final Option optionItem = data[position];

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new OptionHolder();
            row.setTag(holder);
        } else {
            holder = (OptionHolder) row.getTag();
        }

        holder.optionCheckBox   = (CheckBox) row.findViewById(R.id.option_checkbox);
        holder.optionTextView   = (TextView)  row.findViewById(R.id.option_text);

        holder.optionCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {

                    if(isChecked){
                        checkboxCounter++;
                        optionItem.checkFlag = true;
                    } else{
                        checkboxCounter--;
                        optionItem.checkFlag = false;
                    }
                    arrayOptions[position] = new Option(optionItem.id, optionItem.option, optionItem.questionId, optionItem.questionType, optionItem.checkFlag);
            }
        });

        //This is for the checking when I'm from Confirmation activity
        //If optionItem.checkFlag is true, check the checkbox in this position or index
        //Otherwise, else
        if (optionItem != null) {
            holder.optionTextView.setText(optionItem.option);
            if (optionItem.checkFlag) {
                holder.optionCheckBox.setChecked(true);
            } else {
                holder.optionCheckBox.setChecked(false);
            }
        }

        return row;
    }
}

Note: This OptionAdapter class is inside my Answer activity. I have a hunch this caused the bug, nevertheless, it's just a hunch.

Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62

0 Answers0