How to create a Listview in a Navigation Drawer?

Asked

Viewed 607 times

1

1

My goal is this: instead 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 an Object list). But it didn’t work anyway.

Follows 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>

  • Either follow the default Navigation Drawer layout or want a custom layout for you?

  • I will follow the default layout.

1 answer

0

Browser other questions tagged

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