0

I have a LinkedHashMap and I want to insert a List of Strings just to some of the 'headers' of my Expandable List, but for some reason I can't and I get the error

java.lang.IndexOutOfBoundsException: Invalid index 10, size is 1
LinkedHashMap <String, List<String>> expandableListDetail2 = new LinkedHashMap<String, List<String>>();

        List<String> category_header = new ArrayList<String>();
        List<Producto> produtos;
        Database db = new Database(context);
        List<Categorias> categories;

        categories = db.getAllCategorias();

        for (int i=0; i<categories.size(); i++){

            expandableListDetail2.put(categories.get(i).toString(), category_header);

            produtos = db.getAllProductos(categories.get(i).toString());


               for(int j=0;j<produtos.size();j++) {
                   category_header.add(i, produtos.get(i).getNameProducto());
               }

        }

I'm getting error on the line where I have category_header.add(...) and all the products are been added to all keys. Can you help?

1
  • 2
    Nit: you don't need to have the conditional if (produtos.size() > 0) because the for loop checks it anyway. Commented Oct 25, 2015 at 21:22

1 Answer 1

3

What you're doing is using i where you should use j

if (produtos.size() > 0) {
   for(int j=0;j<produtos.size();j++) {
       category_header.add(i, produtos.get(j).getNameProducto());
   }
}

Also it is not neccessary to have an extra if (produtos.size() > 0) { because if produtos.size() is zero the for loop will iterate zero times anyway.

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

5 Comments

No..because I want to insert my list of products a specific category. Not all categories have products...so I have 'i' to identify the correct category to insert my products. And changing it to j, as you said I also get the error.
@porthfind Surely you can't use produtos.get(i).getNameProducto() with index i beacuse that will cause an exception.
You are right, I though you were talking about first 'i' on the category_header.add. But even so I go on with the same error, with another index...Invalid index 10, size is 0
The problem is on "produtos.get(j).getNameProducto()" but I don't know why :\
@porthfind It could be in the getNameProducto function.

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.