Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Gian Lazzarini
Gian Lazzarini
3,263 Points

How do I open specific activities when selecting from an array in a navigation drawer?

I want to be able to open a different activity for "Log History", "New Log", "Analytics", & "Settings"

private void addDrawerItems() {
        String[] osArray = { "Log History", "New Log", "Analytics", "Settings"};
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
        mDrawerList.setAdapter(mAdapter);

        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, NewLogActivity.class);
                startActivity(intent);
            }
        });
    }

1 Answer

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hi Gian! In addition to your String array, try creating an array of classes as well (e.g. Class[]). Then you'd just need to launch the correct Class in onItemClick.