1

I have an alertdialog with multiple choices, I store the user's choices on an ArrayList of strings, and I want to pass the stored arraylist to the host activity (I will use the array's elements to query my database)..

When i run my app, i get an ArrayIndexOutOfBoundsException (may be the index is -1..), I'm not sure if it's the loop, or if i did not pass the arraylist correctly from the alertdialog...

can you guys take a look ? here is my function :

    public void onOkay(ArrayList<String> selected) {
    StringBuilder stringBuilder = new StringBuilder();
    if (selected.size() != 0) {
        for (int i = 0; i < selected.size(); i++) {
            String categories = selected_items_array[selected.indexOf(i)];
            stringBuilder = stringBuilder.append(" " + categories);
        }
        Toast.makeText(this, "You have selected: "
                + stringBuilder.toString(), Toast.LENGTH_SHORT).show();
    }

}

logcat :

java.lang.ArrayIndexOutOfBoundsException: length=6; index=-1
        at com.hichamridouane.smartshop.MainActivity.onOkay(MainActivity.java:164)
        at com.hichamridouane.smartshop.TimelineSettings$2.onClick(TimelineSettings.java:71)

here is my dialogfragment class. and here is my host activity.(as I said, i'm not sure if i'm passing correctly the arraylist to the host activity) thanks !

5
  • 1
    What is selected_items_array ? Commented Nov 18, 2014 at 10:38
  • check both size like selected_items_array,ArrayList<String> selected Commented Nov 18, 2014 at 10:43
  • @SilentKiller it's an array of strings private String[] selected_items_array; Commented Nov 18, 2014 at 13:21
  • 1
    Have you solved your problem? Commented Nov 19, 2014 at 14:50
  • 1
    @joao2fast4u yes I did :) I used arrays of integers in both classes. i'll post my full code asap. thanks btw Commented Nov 20, 2014 at 9:58

2 Answers 2

1

It looks really strange to me, especially in

String categories = selected_items_array[selected.indexOf(i)];

From JavaDocs about indexOf

Returns the index of the first occurrence of the specified element
in this list, or -1 if this list does not contain the element.
More formally, returns the lowest index <tt>i</tt> such that
<tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
or -1 if there is no such index.

So, you try to find element in your selected_items_array (not correct name in Java) in first iteration i == 0, selected_items_array have no such element => indexOf return -1. Array can't have element with index = -1, it starts from 0. So you have your ArrayIndexOutOfBoundsException

Sign up to request clarification or add additional context in comments.

5 Comments

do you have an idea how to solve this ? (and yes the selected_items_array is a dumb name..was planning on changing it later)
I really misunderstand the sense of it... what are you trying to do here String categories = selected_items_array[selected.indexOf(i)]; ??
I mean, you have a collection of Strings (maybe it's something like "category1", "category2", etc.) and then try to find int i in this collection and index of it element to find another element by this index in another Array. It seems like wrong logic here.
yeah it's pretty messed up..passing from an arraylist to another one, then to a string[].. I will correct that and see what happens (I will use an array of integer and pass the indexes to the host activity..instead of passing the values of an array of strings)
add condition: if (selected.indexOf(i) != -1 && selected_items_array.size() >= selected.indexOf(i))
0

Problem solved. Had to use Arraylists of integers in my activity and my dialogfragment.

here is what i did in my DialogFragment class:

public class TimelineSettings extends DialogFragment {
ArrayList<Integer> selected_categories = new ArrayList<Integer>();
boolean[] itemsChecked = {false, false, false, false, false, false};


// this interface to communicate with the host activity.

public interface dialoglistener {
    public void onOkay(ArrayList<Integer> selected);
    public void onCancel();
}
dialoglistener mlistener;


//this function is to instantiate the dialoglistener

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mlistener = (dialoglistener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement dialogListener");
    }
}

My multichoice dialog :

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    for(int i=0;i<itemsChecked.length;i++){
        if(selected_categories.contains((String)String.valueOf(i)))
            itemsChecked[i]=true;
    }

    // Set the dialog title
    builder.setTitle("Choisissez vos paramètres")
            .setMultiChoiceItems(R.array.categories, itemsChecked,
                    new DialogInterface.OnMultiChoiceClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int indexselected,
                                            boolean isChecked) {
                            if (isChecked) {
                                // If the user checked the item, add it to the selected items
                                if(!selected_categories.contains(indexselected)){
                                    selected_categories.add(indexselected);
                                    itemsChecked[indexselected]=true;
                                }
                            } else if (selected_categories.contains(indexselected)) {
                                // Else, if the item is already in the array, remove it
                                selected_categories.remove(indexselected);
                                itemsChecked[indexselected]=false;
                            }
                        }
                    })
                    // Set the action buttons
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    mlistener.onOkay(selected_categories);
                }
            })
            .setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    mlistener.onCancel();
                }
            });
    return builder.create();
}

On my host activity, I implemented the fragment's interface :

    @Override
public void onCreate(Bundle savedInstanceState) {
    /* some fancy stuff */
    Resources res = getResources();
    selectedArray = res.getStringArray(R.array.categories);
}

Getting the selected items (and show them on a toast, just for testing) :

    @Override
public void onOkay(ArrayList<Integer> selected) {
    StringBuilder stringBuilder = new StringBuilder();
    if (selected.size() != 0) {
        for (int i = 0; i < selected.size(); i++) {
            String categories = selectedArray[selected.get(i)];
            stringBuilder = stringBuilder.append(" " + categories);
        }
        Toast.makeText(this, "You have selected: "
                + stringBuilder.toString(), Toast.LENGTH_SHORT).show();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.