Add items to an Expandable List

Asked

Viewed 54 times

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).

  • @jbueno my Dice starts at 0 (int j=0).

  • 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()?

  • Are you sure the mistake is in produtos.get(j).getNomeProducto()?

  • @jbueno is putting products.size() because I think that’s what makes sense...

  • @ramaral works up to Log. d("Expan","get->"+products.get(0). getNomeProduct());

  • Take off the i of add in categoria_header.add(i,produtos.get(j).getNomeProducto());

  • @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..

  • The first category has products?

  • @ramaral, No..

Show 5 more comments

1 answer

0


The mistake happens when you do the add() on the list categoria_header. If the first category has no products nothing is placed on the list, the i is increased, passes to 1, when trying to add i = 1 makes a mistake because i shall not be greater than size of the list.

If I understand what you want to do, alter the code like this:

LinkedHashMap <String, List<String>> expandableListDetail2 = new LinkedHashMap<String, List<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++){

        List<String> categoria_header = new ArrayList<String>();

        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(produtos.get(j).getNomeProducto());
       }

        expandableListDetail2.put(categorias.get(i).toString(), categoria_header);

    }
}

The Hashmap expandableListDetail2 should receive a new list categoria_header for each category. Therefore a new object should be created within the cycle for(i=0; i<categorias.size(); i++)

Note: I think I should change the name categoria_header for categoriaProducts

  • You’re right...I started reading the description and I didn’t even see the code....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.