neo
neo

Reputation: 177

OnCheckedChangedListener Error

I'm not sure what I'm doing wrong here. Getting indexOutOfBounds error below. It is coming from my adapter.java, I'm not 100% it's all correct. I think there are some pieces relating to getting radiogroup states that I'm not familiar with. Any help is appreciated.

08-08 13:36:25.939    1930-1930/com.example.x.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.x.myapp, PID: 1930
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.set(ArrayList.java:481)
        at com.example.x.myapp.ListCarsAdapter$1.onCheckedChanged(ListCarsAdapter.java:80)
        at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
        at android.widget.RadioGroup.access$600(RadioGroup.java:54)
        at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358)
        at android.widget.CompoundButton.setChecked(CompoundButton.java:155)
        at android.widget.CompoundButton.toggle(CompoundButton.java:112)
        at android.widget.RadioButton.toggle(RadioButton.java:78)
        at android.widget.CompoundButton.performClick(CompoundButton.java:124)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

and my adapter.java code is:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v = convertView;
    final ViewHolder holder;
    if(v == null) {
        v = mInflater.inflate(R.layout.list_item_car, parent, false);
        holder = new ViewHolder();
        holder.txtCarName = (TextView) v.findViewById(R.id.txt_car_name);
        holder.radioGroup = (RadioGroup) v.findViewById(R.id.scale);

        holder.radioGroup.setTag(position);
        v.setTag(holder);
    }
    else {
        holder = (ViewHolder) v.getTag();
    }

    // fill row data
    final Car currentItem = getItem(position);
    if(currentItem != null) {
        holder.txtCarName.setText(currentItem.getCar());
        holder.radioGroup.setTag(currentItem.getAnswer());
    }

    holder.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        List<Integer> x = new ArrayList<Integer>();

        public void onCheckedChanged(RadioGroup group, int checkedId) {
            x.set(position, checkedId);

            for (int i = 0; i < x.size(); i++) {
                Log.v("Log", "" + x.get(i));
             }
       }
    });
    return v;
}

Upvotes: 1

Views: 249

Answers (2)

user3956566
user3956566

Reputation:

Your array is empty.

IndexOutOfBoundsException: Invalid index 0, size is 0

Which is failing here:

for (int i = 0; i < x.size(); i++) {
       Log.v("Log", "" + x.get(i));
    }

For List<Integer> x = new ArrayList<Integer>(); is not initialized by:

 x.set(position, checkedId);

Should be:

x.Add(postion, checkedId);

Upvotes: 1

SaNtoRiaN
SaNtoRiaN

Reputation: 2202

The error is in this part

List<Integer> x = new ArrayList<Integer>();
x.set(position, checkedId);

The list size is 0, so you can't replace the item at the index position as it's out of bounds.

Upvotes: 2

Related Questions