其他分享
首页 > 其他分享> > 导航抽屉刷新/重新加载

导航抽屉刷新/重新加载

作者:互联网

我设法使Nav-Drawer和Fragments正常工作.我面临的问题是,我希望用户向Nav-Drawer中添加新的列表项,而我正在使用Dialog来做到这一点:

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);

关闭对话框后,如何刷新列表视图(导航抽屉).我找不到任何有效的解决方案.未定义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();

解决方法:

重写onPrepareOptionsMenu方法并在其中设置适配器.之后,在列表视图上调用invalidateViews().

标签:listview,navigation-drawer,android
来源: https://codeday.me/bug/20191122/2060562.html