我想在ListView中没有任何项时删除optionMenu(显示为emptyView)吗? [英] I want to remove the optionMenu when there are no items in ListView (emptyView is shown)?

查看:58
本文介绍了我想在ListView中没有任何项时删除optionMenu(显示为emptyView)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将项目添加到sqlite数据库并返回游标的应用,然后将此游标与名为StoreCursorAdapter的自定义CursorAdapter一起使用,以在ListView中显示项目.

I have an app that adds items to an sqlite database and returns a cursor, this cursor is then used with a custom CursorAdapter called StoreCursorAdapter to show the items in a ListView.

有一个(全部删除)按钮作为optionsMenuItem.

There is a (delete all) button as an optionsMenuItem.

当ListView中没有可用项时,我想隐藏此optionsMenuItem.

I want to hide this optionsMenuItem when no items are avaliable in the ListView.

库存活动

EditorActivity

很抱歉,我是新用户,所以他们还不允许我嵌入图像. :-(

Sorry for the links I am a new user so they don't allow me to embed images yet. :-(

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

    //Declare the views
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    ListView list = (ListView) findViewById(R.id.list);
    emptyView = findViewById(R.id.empty_view);

    //Set the screen to be shown when there are no list items
    list.setEmptyView(emptyView);

    //StoreCursorAdapter is a custom CursorAdapter
    mAdapter = new StoreCursorAdapter(this, null);
    list.setAdapter(mAdapter);
}

@Override
protected void onStart() {
    super.onStart();
    invalidateOptionsMenu();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu options from the res/menu/menu_inventory.xml file.
    // This adds menu items to the app bar.
    getMenuInflater().inflate(R.menu.menu_inventory, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // User clicked on a menu option in the app bar overflow menu
    showDeleteConfirmationDialog();
    return super.onOptionsItemSelected(item);
}

我尝试了

1-emptyView.visibilty()== View.INVISBLE

1 - emptyView.visibilty() == View.INVISBLE

2-list.getAdapter ==空

2 - list.getAdapter == null

但是他们没有用

什么陈述能胜任?!

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        if (//what statement) {
            MenuItem menuItem = menu.findItem(R.id.action_delete_all);
            menuItem.setVisible(false);
        }
        return true;
    }

注意:

OnStart()被调用

OnStart() gets called after I get back from the EditorActivity

注意:

在我的应用中,我可以从另一个活动中删除单个项目,因此添加了invalidateOptionsMenu();.在onOptionsItemSelected中不会执行此操作.

In my app I can delete individual items from another activity so adding invalidateOptionsMenu(); in the onOptionsItemSelected won't do the job.

推荐答案

放入onPrepareOptionsMenu的正确条件是:

menuItem.setVisible(!mAdapter.isEmpty());

与ListView使用相同的比较来管理空视图(减去null检查)(

that is the same comparison the ListView uses manages the empty view (minus null check) (https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/AdapterView.java#747)

但是我相信这里还有另一个问题,就是在showDeleteConfirmationDialog()期间,活动开始后数据正在更改.这意味着您必须在数据更改时调用invalidateOptionsMenu().有两种方法可以做到这一点.一种更健壮,另一种则更快地进行编码:

but I believe there's another issue here, is that the data is changing after the activity started, during showDeleteConfirmationDialog(). That means you have to call invalidateOptionsMenu() the moment the data is changed. There're two ways of doing it. One more robust and the other is faster to code:

  1. 编写代码更快(但不是很好/干净):

在执行DB操作的代码之后

添加invalidateOptionsMenu().

add invalidateOptionsMenu() after the code that executes the DB operations.

  1. 更坚固/更干净

您将使用开始/停止回调来侦听数据中的更改.类似于以下内容:

you'll use start/stop callbacks to listen to changes in the data. Something like the following:

@Override protected void onStart(){
  super.onStart();
  invalidateOptionsMenu();
  mAdapter.registerDataSetObserver(dataObserver);
}

@Override protected void onStop(){
  mAdapter.unregisterDataSetObserver(dataObserver);
  super.onStop();
}

private final DataSetObserver dataObserver = new DataSetObserver(){
  @Override public void onChanged(){
    invalidateOptionsMenu();
  }
  @Override public void onInvalidated(){
    invalidateOptionsMenu();
  }
};

上面的所有代码都是用心输入的,可能会有错别字,但这就是想法和错别字,您以后可以在代码中修复.

all code above was typed by heart, there're likely typos, but that's the idea and the typos you can fix later at your code.

这篇关于我想在ListView中没有任何项时删除optionMenu(显示为emptyView)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆