简体   繁体   中英

Android - How to refresh the menu item of Navigation Drawer

Well first of all most of the code is from here .

Ok, so there's a code for activity that creates a Navigation Drawer, The Navigation Drawer menu item includes a imageview a text and a number counter.

Now the thing I'm trying to understand is how can I make the number counter get changed acoording to data from the db.

So here's the code -

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nav_activity_main);

    mTitle = mDrawerTitle = getTitle();

    // load slide menu items
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

    // nav drawer icons from resources
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);

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

    navDrawerItems = new ArrayList<NavDrawerItem>();

    dbHand = new DbHandler(this);
    int numOfBotn = dbHand.queryAllBotnstonum();
    String numBotn = String.valueOf(numOfBotn);

    int numOfgROUPmSG = dbHand.newAllNewGroupMessages();
    String numNewgRmsg = String.valueOf(numOfgROUPmSG);

    lhand = new LocalStorageHandler(this);
    int newprasoMsg = lhand.gotAllNewMsg();
    String numNewPersonalMsg = String.valueOf(newprasoMsg);
    // adding nav drawer items to array
    // Home
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1), true, numNewPersonalMsg));
    // Find People
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1), true , numNewgRmsg));
    // Photos
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1), true, numBotn));
    // Communities, Will add a counter here
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
    // Pages
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
    // What's hot, We  will add a counter here
    //navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));


    // Recycle the typed array
    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    // setting the nav drawer list adapter
    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);

    // enabling action bar app icon and behaving it as toggle button
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.button_positive_off));

    //getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, //nav menu toggle icon
            R.string.app_name, // nav drawer open - description for accessibility
            R.string.app_name // nav drawer close - description for accessibility
    ) {
        public void onDrawerClosed(View view) {
            Log.d("OUT", "CLOSED");
            getActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {

            getActionBar().setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        // on first time display view for first nav item
        displayView(0);
    }

Now how can i set again the adapter for the mDrawerList .

I understand that onDrawerOpened can be a good place to do it.

What kind of code am I need to use in order to that?

Thanks for any kind of help.

Now how can i set again the adapter for the mDrawerList.

You should not. Instead, you could simply call Adapter:notifyDatasetChanged() if adapter's data has changed

I understand that onDrawerOpened can be a good place to do it.

Not sure, as it represents a Drawer in a settled state, that is - visible to user. Any changes happening from this moment on would seem strange or potentially slow down user experience, or both. I guess, though, that for the sake of simplicity you could try it .

A better solution would require you to implement your own Loader and a content observer that would trigger events according to changes made to your underlying datasource. And that is a bit of a challenge, if you are not familiar with these constructs yet.

What kind of code am I need to use in order to that?

If you intend to update your navdrawer with info from database, it would be nice if your NavDrawerListAdapter would subclass CursorAdapter .

If you manage to successfully change your adapter code to work with Cursor instead of/in addition to hard-coded elements, the rest of your task is just as simple as:

  1. Query your database for info. The result will be a Cursor pointing to your data

  2. Set the new Cursor to your NavDrawerListAdapter and call notifyDatasetChanged() on this adapter

Again, it would be better if this "loading" part would happen asynchronously, in an AsyncTaskLoader, for instance, or in AsyncTask (less preferable if you do not properly handle configuration changes).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM