0
I’m trying to use an Expandable List. In this Expandable List I intend to put as header "the categories of my products" and in the children items put "my products".
However I’m getting the error "java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
", when I try to insert the products into the categories(headers). Both the products and the categories I get them from my database.
LinkedHashMap <String, List<String>> expandableListDetail2 = new LinkedHashMap<String, List<String>>();
List<String> categoria_header = new ArrayList<String>();
int i=0;
List<Producto> produtos;
Database db = new Database(context);
List<Categorias> categorias;
categorias = db.getAllCategorias();
for (i=0; i<categorias.size(); i++){
expandableListDetail2.put(categorias.get(i).toString(), categoria_header);
produtos = db.getAllProductos(categorias.get(i).toString());
for(int j=0;j<produtos.size();j++) {
Log.d("Expandable","categoria->"+categorias.get(i).toString());
Log.d("Expan","get->"+produtos.get(0).getNomeProducto());
categoria_header.add(i,produtos.get(j).getNomeProducto());
}
}
The problem is in produtos.get(j).getNomeProducto()
.
Anyone can help?
Produtos
is a list with 1 item and you are trying to access the index 1 (second position).– Jéf Bueno
@jbueno my Dice starts at 0 (int j=0).
– porthfind
I saw that. I just said what the error says, but it really doesn’t seem to make much sense. You can put in the question the value of
produtos.size()
?– Jéf Bueno
Are you sure the mistake is in
produtos.get(j).getNomeProducto()
?– ramaral
@jbueno is putting products.size() because I think that’s what makes sense...
– porthfind
@ramaral works up to Log. d("Expan","get->"+products.get(0). getNomeProduct());
– porthfind
Take off the
i
of add incategoria_header.add(i,produtos.get(j).getNomeProducto());
– ramaral
@ramaral, if I take the i, I’ll be adding my products to all categories and I don’t want that, I want to add the commodities to the categories they correspond to. I don’t know if I made myself clear..
– porthfind
The first category has products?
– ramaral
@ramaral, No..
– porthfind