Tell me how to make a dynamic list with items in NavigationView. I tried to do through ListView simply by placing the ListView in the markup of the NavigationView template and filling it through the adapter, but the application crashes with the error:
Standard template in NavigationView widget inside NavigationDrawer:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent"/> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer"/> </android.support.v4.widget.DrawerLayout> Menu items are defined in the menu resource file and assigned to NavigationView in the line:
app:menu="@menu/activity_main_drawer" Here is the file @ menu / activity_main_drawer:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_camera" android:icon="@drawable/ic_menu_camera" android:title="Import"/> <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_menu_gallery" android:title="Gallery"/> <item android:id="@+id/nav_slideshow" android:icon="@drawable/ic_menu_slideshow" android:title="Slideshow"/> <item android:id="@+id/nav_manage" android:icon="@drawable/ic_menu_manage" android:title="Tools"/> </group> <item android:title="Communicate"> <menu> <item android:id="@+id/nav_share" android:icon="@drawable/ic_menu_share" android:title="Share"/> <item android:id="@+id/nav_send" android:icon="@drawable/ic_menu_send" android:title="Send"/> </menu> </item> </menu> Instead of these items, I tried to create a dynamically created ListView just by inserting it there:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <ListView android:id = "@+id/listView" android:layout_width = "match_parent" android:layout_height = "match_parent"/> </menu> And initialize via the adapter from onCreate ():
ListView listView = ( ListView ) findViewById( R.id.listView ); final String[] sets = new String[] { "one", "two", "three", "four", "five" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, sets ); listView.setAdapter( adapter ); The application crashes with an error. In fact, it is clear that while NavigationView is not open, the ListView does not exist and it will not be possible to initialize a non-existing element when creating activity.
layout- Vladyslav Matviienko