1

please help me. I am beginner in android java and I stuck with this code.

I have a list item and I want to put exit app button/menu on this. I just need to run a function to exit app AppExit();

My xml file:

<string-array name="navigation_title_list">
    <item>Home</item>
    <item>Our Products</item>
    <item>References</item>
    <item>Support</item>
    <item>About Us</item>
    <item>Contact</item>
    <item>Exit</item>
</string-array>

My java file:

   // reference
        mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_main_layout);
        mDrawerListView = (ListView) findViewById(R.id.activity_main_drawer);

        // set drawer
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        mDrawerListView.setAdapter(new DrawerAdapter(this, mTitles, icons));
        mDrawerListView.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View clickedView, int position, long id)
            {

                // Here is my exit funtion
                AppExit();

                selectDrawerItem(position, false);
            }
        });

I need code something like:

if(<!-- Item value selected == "Exit" -->){
// run exit app funtion
AppExit();
}     

Any help will be appreciated. thanks.

1 Answers1

1

You have mTitles, which I assume is the string array, and in the callback, you have a position argument which tells you which item in the list was clicked. Therefore, if you check mTitles[position] for the string "Exit", it should work.

if (mTitles[position].equals("Exit")) {
    AppExit();
}
Alan
  • 2,962
  • 2
  • 15
  • 18