Skip to content Skip to sidebar Skip to footer

Navigation Drawer Listview Is Empty

I have an app with the first activity being a log-in page. When the user successfully logs in he sees his projects. I am using ListFragment to diplay this list(since I also have pu

Solution 1:

I suggest to you to do it like following:

In the DrawerActivity, create only the navigation drawer, without using onCreate(), but using onStart(). The onCreate() will be called in the activity that extends DrawerActivity and there you'll call setContentView

Your class will look like so:

publicclassDrawerActivityextendsActivity {
privateDrawerLayout mDrawerLayout;
privateListView mDrawerList;
privateActionBarDrawerToggle mDrawerToggle;

privateCharSequence mDrawerTitle;
privateCharSequence mTitle;
privateString[] mScreenTitles;

@OverrideprotectedvoidonStart(){
        super.onStart();
        mTitle = mDrawerTitle = getTitle();
        mScreenTitles = getResources().getStringArray(R.array.screen_titles);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        mDrawerList.setAdapter(newArrayAdapter<String>(this,
                R.layout.drawer_list_item, mScreenTitles));
        //mDrawerList.setOnItemClickListener(new DrawerItemClickListener());// enable ActionBar app icon to behave as action to toggle nav drawergetActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions// between the sliding drawer and the action bar app icon
        mDrawerToggle = newActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_navigation_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 */
        ) {
            publicvoidonDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            publicvoidonDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

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

/* Called whenever we call invalidateOptionsMenu() */@OverridepublicbooleanonPrepareOptionsMenu(Menu menu) {
    // If the nav drawer is open, hide action items related to the content viewreturnsuper.onPrepareOptionsMenu(menu);
}

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
    // The action bar home/up action should open or close the drawer.// ActionBarDrawerToggle will take care of this.if (mDrawerToggle.onOptionsItemSelected(item)) {
        returntrue;
    }
    returnsuper.onOptionsItemSelected(item);
}

/* The click listner for ListView in the navigation drawer */privateclassDrawerItemClickListenerimplementsListView.OnItemClickListener {
    @OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

privatevoidselectItem(int position) {

    }
}

In every activity in which you want to use the Drawer, you have to build you layout like you current private_project_list.xml, using DrawerLayout as main container, the doby layout of your activity as its first child and the navigation drawer layout as second child. Then, extends the activity to DrawerActivity.

Antoher suggestion is to get the list in teh DrawerActivity, calling HttpGetHandler() inside of it, you have to set the adapter only when the array will be full, otherwise you pass to the adapter an empty array, so the list will be empty.

About the menu button not displayed

In DrawerActivity seems missing some methods for the action bar, try add the following

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

    @OverridepublicvoidonConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

Post a Comment for "Navigation Drawer Listview Is Empty"