android SimpleCursorAdapter 没有项目消息

发布于 2024-10-31 19:09:55 字数 2300 浏览 5 评论 0原文

我习惯使用以下代码来显示最喜欢的项目列表。它具有通过上下文菜单的删除功能。

@Override
    public void onCreate(Bundle savedInstanceState) {

    .........................
    .........................

       wordDataHelper = new WordDataHelper(getApplicationContext());
        favCursor  = wordDataHelper.getCursorFav();
        startManagingCursor(favCursor);
        // Now create a new list adapter bound to the cursor.
        // SimpleListAdapter is designed for binding to a Cursor.
        favAdapter = new SimpleCursorAdapter(
                this, // Context.
                android.R.layout.simple_list_item_1,
                favCursor,                                              // Pass in the cursor to bind to.
                new String[] {WordDataHelper.ENGWORD},           // Array of cursor columns to bind to.
                new int[] {android.R.id.text1});  // Parallel array of which template objects to bind to those columns.

        // Bind to our new adapter.
        setListAdapter(favAdapter);

        list = getListView();


list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
            // @Override
            public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenu.ContextMenuInfo menuInfo) {
                menu.setHeaderTitle("Context Menu");
                menu.add(0, CONTEXT_DELETE, 1, "Delete Item");
            }
        });

        list.setTextFilterEnabled(true);

        list.setClickable(true);

        ..................
    ..................
}

public boolean onContextItemSelected(MenuItem item) {

        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item
                .getMenuInfo();
        final Long wordId = menuInfo.id;
        // selected_row = menuInfo.position;

        // To get the id of the clicked item in the list use menuInfo.id
        switch (item.getItemId()) {
        case CONTEXT_DELETE:
            deleteRes(wordId);
            favCursor  = wordDataHelper.getCursorFav();
            ((SimpleCursorAdapter) favAdapter).changeCursor(favCursor);
            break;
        default:
            return super.onContextItemSelected(item);

        }


        return true;
    }

一切正常。现在,我想在没有要列出的项目时显示“没有最喜欢的项目”消息。怎么安排呢?

I have used to following code to display the favorite item listing. It has a deletion functionality through context menu.

@Override
    public void onCreate(Bundle savedInstanceState) {

    .........................
    .........................

       wordDataHelper = new WordDataHelper(getApplicationContext());
        favCursor  = wordDataHelper.getCursorFav();
        startManagingCursor(favCursor);
        // Now create a new list adapter bound to the cursor.
        // SimpleListAdapter is designed for binding to a Cursor.
        favAdapter = new SimpleCursorAdapter(
                this, // Context.
                android.R.layout.simple_list_item_1,
                favCursor,                                              // Pass in the cursor to bind to.
                new String[] {WordDataHelper.ENGWORD},           // Array of cursor columns to bind to.
                new int[] {android.R.id.text1});  // Parallel array of which template objects to bind to those columns.

        // Bind to our new adapter.
        setListAdapter(favAdapter);

        list = getListView();


list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
            // @Override
            public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenu.ContextMenuInfo menuInfo) {
                menu.setHeaderTitle("Context Menu");
                menu.add(0, CONTEXT_DELETE, 1, "Delete Item");
            }
        });

        list.setTextFilterEnabled(true);

        list.setClickable(true);

        ..................
    ..................
}

public boolean onContextItemSelected(MenuItem item) {

        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item
                .getMenuInfo();
        final Long wordId = menuInfo.id;
        // selected_row = menuInfo.position;

        // To get the id of the clicked item in the list use menuInfo.id
        switch (item.getItemId()) {
        case CONTEXT_DELETE:
            deleteRes(wordId);
            favCursor  = wordDataHelper.getCursorFav();
            ((SimpleCursorAdapter) favAdapter).changeCursor(favCursor);
            break;
        default:
            return super.onContextItemSelected(item);

        }


        return true;
    }

Everything is working fine. Now I want to display a "No favorite item" msg when there is no item to list. How to arrange it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文