3

In my Navigation Drawer I have a problem, when it loads anything in the first spot will crash the app, so my solution to this is to set it to a string that changes to say "(appname) Free" or " (appname) Premium", depending on if the premium upgrade was purchased.

I would like this unclickable as it is currently able to be clicked but nothing happens. Ideally this would be a submenu, or title, but I could not figure out how to implement that. Here is a excerpt of my code:

public class myClass extends SherlockActivity implements
    OnItemSelectedListener {

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;


@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    mTitle = mDrawerTitle = getTitle();

    if (mIsPremium == true) {
        mPlanetTitles = getResources().getStringArray(
                R.array.planets_array_prem);
    } else {
        mPlanetTitles = getResources()
                .getStringArray(R.array.planets_array);
    }

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer
    // opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);
    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPlanetTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
    mDrawerLayout, /* DrawerLayout object */
    R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
    R.string.drawer_open, /* "open drawer" description for accessibility */
    R.string.drawer_close /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(mTitle);
            supportInvalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(mDrawerTitle);
            supportInvalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        selectItem(0);
    }

}

/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements
        ListView.OnItemClickListener {

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

        switch (position) {
        case 0:
            /*
             * Toast.makeText( getApplicationContext(),
             * "case 0 -  A Activity.", Toast.LENGTH_LONG).show(); Intent
             * c0 = new Intent(getBaseContext(),
             * activity.class); startActivity(c0);
             */break;
        case 1:
            Toast.makeText(getApplicationContext(),
                    "case 1 - B Activity", Toast.LENGTH_LONG).show();
            Intent c1 = new Intent(getBaseContext(),
                    ActivityB.class);
            startActivity(c1);
            break;
        default:
        }
    }
}

public void selectItem(int position) {
    switch (position) {
    case 0:
        // setContentView(R.layout.main);
        break;
    case 1:
        setContentView(R.layout.main);
        break;
    default:
    }
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getSupportActionBar().setTitle(mTitle);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

I do not use fragments, I was going to implement the navigation drawer by doing on click and moving to new activities. I am looking for a solution to my primary problem but would welcome any design tips.

Thank you

Edit

With CommonsWare's help I developed this: but nothing is different in my application. I want the first time (case 0 (position 0)) to be disabled.

public class MyArrayAdapter extends ArrayAdapter<String> {

    public MyArrayAdapter(Context context, int position) {
        super(context, 0);
    }

    public boolean areAllItemsEnabled() {
        return false;
    }

    public boolean isEnabled(int position) {
        if (position == 0) {
            return false;
        } else {
            return true;
        }
    }

}
user1363871
  • 580
  • 3
  • 11
  • 29

1 Answers1

6

As you already know, since you already read the answer, you need to replace your new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles) with a custom Adapter -- perhaps one extending ArrayAdapter<String> -- where you override areAllItemsEnabled() to return false and isEnabled() to return true or false as needed.

See also:

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yes, I did see those answers but I was stumped as to how I would make this happen. I am a novice and stumble through some things and need help with others, this is one of them. I appreciate your answer and will attempt it, but if you have an example that would be superb. Every one that I have seen always initiates a new class, I would like to keep it in the current class if able. – user1363871 Aug 30 '13 at 00:12
  • @user1363871: "Every one that I have seen always initiates a new class, I would like to keep it in the current class if able" -- you are not able. The only way to override methods is via a subclass. I am unclear what you need in terms of an example that is not covered by the first code snippet in http://stackoverflow.com/a/4636538/115145 – CommonsWare Aug 30 '13 at 00:19
  • I created an ArrayAdapter that extends the String ArrayAdapter. I put in areAllItemsEnabled and isEnabled, the end result is simply nothing is different. I will edit the origional question with the new ArrayAdapter. Thanks for your help. – user1363871 Aug 30 '13 at 03:46
  • @user1363871: Did you switch to using this adapter, instead of the plain `ArrayAdapter` you had been using? My guess is not, as your `MyArrayAdapter` would give you an empty list, as you are not putting any data into it. – CommonsWare Aug 30 '13 at 10:45
  • @user1363871: As I wrote in my answer, you need to replace your `new ArrayAdapter(this, R.layout.drawer_list_item, mPlanetTitles)`. You need `MyArrayAdapter` to have a constructor that passes to the superclass your `R.layout.drawer_list_item` and `mPlanetTitles` values. – CommonsWare Aug 30 '13 at 15:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36564/discussion-between-user1363871-and-commonsware) – user1363871 Aug 30 '13 at 17:54