How to create a ListView in a Navigation Drawer?

1

1

My goal is this: in place of these items that come by default in Navigation Drawer I would like to put a series of lists that will change as I create, delete and edit these lists.

I tried to create an Adapter with an Array of Strings just to test (my real goal is to use a list of Objects). But it did not work anyway.

Follow the code:

MainActivity.java

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
  private List<ListaDeCompras> listasDeCompras = new ArrayList<ListaDeCompras>();
  private ListaDeCompras listaDeComprasAtual;
  private ListView listaDeComprasView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open,
            R.string.navigation_drawer_open);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    drawer.openDrawer(GravityCompat.START);

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    String[] listas = {"Lista 1", "Lista 2", "Lista 3"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listas);
    listaDeComprasView = (ListView) findViewById(R.id.listas_de_compras);
    listaDeComprasView.setAdapter(adapter);

  }
}

activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nova_lista"
        android:title="Nova Lista" />
</group>

<item android:title="Listas de Compras">
    <menu>
        <ListView
            android:id="@+id/listas_de_compras"
            />
    </menu>
</item>

    
asked by anonymous 02.03.2017 / 20:57

1 answer

0

I think the following code can solve your problem.

Menu menu = navigationView.getMenu();

for (String itemMenu : menus) {
    MenuItem menuItem = menu.add(itemMenu);
    menuItem.setIcon(....);
    menuItem.setTitle(....);
}

The following documentation for the Menu class is MenuItem. link link

    
03.03.2017 / 11:17