How to launch activity from Navigation Drawer?

2019-07-27 02:58发布

So I've searched allot about navigation drawers here, and when I was pointed to a tutorial from an answer in another persons question. I did so.

I successfully managed to create and style the nav drawer to my liking. But now I've been searching tirelessly on how I can launcher activities from the navigation drawer. I've managed to get some code into the the MainActivity but upon clicking the item it does not launch anything? All activities are define in the Manifest. I decided to use Toasts as a trail and error, still no luck.

here's my code for nav drawer, and launch activity.

// Drawer Activity        
// Get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);

// Get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);

// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
// Run Activity from drawer
drawerListView.setOnItemClickListener(new DrawerItemClickListener());

And this is my DrawerItemClickListener method

private class DrawerItemClickListener implements ListView.OnItemClickListener {

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    switch(position) {
      case 0:
          Intent a = new Intent(this, AppInfo.class);
              startActivity(a);
      break;
      case 1:
          Intent b = new Intent(getBaseContext(), WelcomeActivity.class);
           startActivity(b);
            }
        }
  }

2条回答
家丑人穷心不美
2楼-- · 2019-07-27 03:48
Intent abc = new Intent(CurrentActivityName.this,TargetActivityName.class);
startActivity(abc);

This is how I've been doing it, directly referencing each activities name.

查看更多
趁早两清
3楼-- · 2019-07-27 03:50

Repalce this with MainActivity.this like that:

Intent a = new Intent(MainActivity.this, AppInfo.class);
startActivity(a);

Also Change that

drawerListView.setOnItemClickListener(new DrawerItemClickListener());

replace

drawerListView.setOnItemClickListener(this);

Check there for Custom Adapter

查看更多
登录 后发表回答