0

このヘルパー クラスを使用して、アプリでナビゲーション ドロワーを作成しています。json ファイルから動的にデータを追加するために正常に動作しています。

    NavMenuItem createMenu = null;
                NavDrawerItem[] menu = new NavDrawerItem[]{};
                int i = 1;
                for (Post post : PostsActivity.this.posts){
                     createMenu = NavMenuItem.create(i, post.title+i, false, this);
                     menu = new NavDrawerItem[]{createMenu};
                     i++;
                }

NavDrawerActivityConfiguration navDrawerActivityConfiguration = new NavDrawerActivityConfiguration();
            navDrawerActivityConfiguration.setMainLayout(R.layout.activity_drawer);
            navDrawerActivityConfiguration.setDrawerLayoutId(R.id.drawer_layout);
            navDrawerActivityConfiguration.setLeftDrawerId(R.id.nav_drawer);
            navDrawerActivityConfiguration.setNavItems(menu);
            navDrawerActivityConfiguration.setDrawerShadow(R.drawable.drawer_shadow);
            navDrawerActivityConfiguration.setDrawerOpenDesc(R.string.drawer_open);
            navDrawerActivityConfiguration.setDrawerCloseDesc(R.string.drawer_close);
            navDrawerActivityConfiguration.setBaseAdapter(
                    new NavDrawerAdapter(this, R.layout.listview_drawer_item, menu ));

posts問題は、配列の最後のデータのみが表示されることです。すべてのデータを配列に追加するように配列を反復するにはどうすればよいNavDrawerItemですか?

これは、著者によって与えられた元の例でデータが配列に追加される方法です。

NavDrawerItem[] menu = new NavDrawerItem[] {
                NavMenuSection.create( 100, "Demos"),
                NavMenuItem.create(101,"List/Detail (Fragment)", "navdrawer_friends", false, this),
                NavMenuItem.create(102, "Airport (AsyncTask)", "navdrawer_airport", true, this), 
                NavMenuSection.create(200, "General"),
                NavMenuItem.create(202, "Rate this app", "navdrawer_rating", false, this),
                NavMenuItem.create(203, "Eula", "navdrawer_eula", false, this), 
                NavMenuItem.create(204, "Quit", "navdrawer_quit", false, this)};
4

1 に答える 1

1

これを試して:

ArrayList<NavDrawerItem> menu = new ArrayList<NavDrawerItem>();
int i = 1;
for (Post post : PostsActivity.this.posts){
     menu.add(NavMenuItem.create(i, post.title+i, false, this));
     i++;
}
NavDrawerItem[] navItems = new NavDrawerItem[menu.size()];
navItems = menu.toArray(navItems);

NavDrawerActivityConfiguration navDrawerActivityConfiguration = new NavDrawerActivityConfiguration();
navDrawerActivityConfiguration.setMainLayout(R.layout.activity_drawer);
navDrawerActivityConfiguration.setDrawerLayoutId(R.id.drawer_layout);
navDrawerActivityConfiguration.setLeftDrawerId(R.id.nav_drawer);
navDrawerActivityConfiguration.setNavItems(navItems);
navDrawerActivityConfiguration.setDrawerShadow(R.drawable.drawer_shadow);
navDrawerActivityConfiguration.setDrawerOpenDesc(R.string.drawer_open);
navDrawerActivityConfiguration.setDrawerCloseDesc(R.string.drawer_close);
navDrawerActivityConfiguration.setBaseAdapter(
        new NavDrawerAdapter(this, R.layout.listview_drawer_item, navItems ));
于 2013-11-12T22:46:44.660 に答える