I have this function that is responsible for filling the Navigation View and it works like a charm, but the problem is how can I give each Navigation element an activity. Thanks in advance.
private void addItemsRunTime(NavigationView navigationView) {
//CARGAR MENU DE NAVEGACION
final Menu menu = navigationView.getMenu();
bd = new BD(MainActivity.this, "appLuisGonzalez", null, 2);
SQLiteDatabase db = bd.getWritableDatabase();
String[] campos = new String[] {"id_categoria", "nombre", "slug"};
if (db != null){
Log.e("1","");
//Cursor c = db.rawQuery("SELECT * FROM productos", null);
Cursor c = db.query("categoria", campos, null, null, null, null, null);
SubMenu subMenu = null;
try{
if (c.moveToFirst()) {
//Recorremos el cursor hasta que no haya más registros
do {
int cont = 0;
Cursor cursorCategoriaHijos = db.rawQuery(" SELECT id_categoria, nombre, slug FROM categoria_hijos WHERE id_padre='"+c.getString(0)+"' ", null);
if (cursorCategoriaHijos.moveToFirst()){
do{
if (cont == 0)
subMenu = menu.addSubMenu(c.getString(1).toUpperCase());
subMenu.add(cursorCategoriaHijos.getString(1));
cont++;
}while (cursorCategoriaHijos.moveToNext());
}
else{
menu.add(c.getString(1));
}
} while(c.moveToNext());
}
db.close();
}catch (Exception e){
Log.e("Error:", "Error al leer datos de la base de datos");
}
}
else
Toast.makeText(getBaseContext(),"Error de Conexion a la base de datos local, Vuelva actualizar la Aplicación", Toast.LENGTH_SHORT).show();
// refreshing navigation drawer adapter
for (int i = 0, count = navigationView.getChildCount(); i < count; i++) {
final View child = navigationView.getChildAt(i);
if (child != null && child instanceof ListView) {
final ListView menuView = (ListView) child;
final HeaderViewListAdapter adapter = (HeaderViewListAdapter) menuView.getAdapter();
final BaseAdapter wrapped = (BaseAdapter) adapter.getWrappedAdapter();
wrapped.notifyDataSetChanged();
}
}
}
The method
SubMenu.add(String nombre)
returns an object of type to youMenuItem
for good reason. In your code you can add: