ARP
ARP

Reputation: 613

ArrayOut of Bound exception in Android while working with grid view

@Override
public View getView(final int position, View view, ViewGroup parent) {
     view = _lInflater.inflate(R.layout.all_apps_checked_item, null);
     CheckBox chkUhkItem = (CheckBox) view.findViewById(R.id.all_apps_cb);
     chkUhkItem.setOnCheckedChangeListener(new OnCheckedChangeListener() {

         @Override
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             _iCheckBoxClick.onCheckCliked(isChecked,_apps.get(position).packageName,position);
         }
      });
      return view;
}

And my public interface is

public interface ICheckBoxClick{
    void onCheckCliked(boolean isChecked,String packageName,int pos);
}

And my implementation is for ICheckBoxClick is:

//Apps Seletion Dialog
public void showSeletionAppsDialog(Context actityCtx,
        final String cat_name, final IAddSubItemsClick iAddSubItemsClick) {
    final ArrayList<String> selected = new ArrayList<String>();
    final Dialog dialog = new Dialog(actityCtx);
    dialog.setTitle("Select Apps =>" + cat_name);
    dialog.setContentView(R.layout.all_apps_grid);
    GridView appsGrid = (GridView) dialog.findViewById(R.id.all_apps);
    ICheckBoxClick icheckBoxClick = new ICheckBoxClick() {

        @Override
        public void onCheckCliked(boolean isChecked, String pkgName, int pos) {
            if (isChecked) {
                selected.add(cat_name + "," + pkgName);
            } else {
                selected.remove(pos);
            }
        }
    };
    appsGrid.setAdapter(new AllAppsSelectionAdapter(actityCtx,
            icheckBoxClick));
}

Selected.remove(pos) is throwing Array out of bound exception.

12-30 12:59:18.106: E/AndroidRuntime(13028): java.lang.IndexOutOfBoundsException: Invalid index 7, size is 1
12-30 12:59:18.106: E/AndroidRuntime(13028):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
12-30 12:59:18.106: E/AndroidRuntime(13028):    at java.util.ArrayList.remove(ArrayList.java:403)

From the above logs I understood that size of ArrayList is only 1 and you are trying to remove & element. Hence arrayOutOfBoundException. I don't have better solution to fix this.

Upvotes: 1

Views: 622

Answers (3)

ARP
ARP

Reputation: 613

Finally the solution. Rather than looking at indices of arraylist ,it is better to remove objects from array list by specifying specific object.

Upvotes: 0

sotondolphin
sotondolphin

Reputation: 223

It seems the object that calls getView() has passed in unexpected position index.

Upvotes: 0

kalyan pvs
kalyan pvs

Reputation: 14590

use remove(Object object) method instead remove(int index)

The problem is here..

    if (isChecked) {
            selected.add(cat_name + "," + pkgName);
        } else {
            selected.remove(pos);
        }

you are adding String to Araylist...but you are removing int from the Arraylist..if you added first item in listview..there is only one item..if you added 5th item then there is two elements..if you remove 5th element you are removing remove(5) but there are only two elements..so use remove(object object) method instead remove(int index) change your code like..

 if (isChecked) {
            selected.add(cat_name + "," + pkgName);
        } else {
            selected.remove(cat_name + "," + pkgName);
        }

Upvotes: 2

Related Questions