导航抽屉刷新/重新加载

时间:2013-11-05 21:56:04

标签: android listview navigation-drawer

我设法让Nav-Drawer和Fragments很好地工作。我面临的问题是,我希望用户将新的列表项添加到Nav-Drawer中,并且我使用对话框执行此操作:

public void createAlert() {
    //ALERT
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Neue Liste erstellen");
    //alert.setMessage("Message");

    // Set an EditText view to get user input
    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String value = input.getText().toString();
            // Do something with value!

            dbHelper.createNewListItem(value, null);

        }
    });
}

我的导航抽屉看起来像这样:

    // Erstellt den neuen MenuAdapter aus der Klasse MenuListAdapter
    MenuListAdapter mMenuAdapter = new MenuListAdapter(this, drawerTitles,  drawerSubtitles, drawerIcons);

    View footer = (View)getLayoutInflater().inflate(R.layout.drawer_list_header, null);
    mDrawerList.addFooterView(footer);

    mDrawerList.setAdapter(mMenuAdapter);

关闭对话框后,如何刷新列表视图(nav-drawer)。我找不到任何有效的解决方案。 notifyDataSetChanged未定义,且这些都不起作用:

mDrawerList.refreshDrawableState();
mDrawerList.invalidate();

任何人都知道解决方案吗?

更新

这是我的重装方法:

for(int i = 0; i < cursor.getCount(); i++){
    String listname = cursor.getString(cursor.getColumnIndex("listname"));
    String listsubtitle = cursor.getString(cursor.getColumnIndex("subtitle"));
    allListNames[i] = listname;
    allListSubtitle[i] = listsubtitle;
    cursor.moveToNext();
    }

    drawerTitles = allListNames;
    drawerSubtitles = allListSubtitle;

    mMenuAdapter = new MenuListAdapter(this, drawerTitles, drawerSubtitles,   drawerIcons);  
    mDrawerList.setAdapter(mMenuAdapter);
    mMenuAdapter.notifyDataSetChanged();

2 个答案:

答案 0 :(得分:3)

覆盖onPrepareOptionsMenu方法并在其中设置适配器。之后,在列表视图中调用invalidateViews()

答案 1 :(得分:0)

您可以从ur navigatioView objct调用该项目。

    navigationView.getMenu().findItem(R.id.navigation_item_2).setTitle("Hello");