Android spinner - 无法静态引用非静态方法帮助

发布于 2024-10-21 11:59:56 字数 1346 浏览 1 评论 0原文

我按照教程将 SQLite 数据库中的数据填充到 Android 中的微调器(下拉列表)中。但是,我收到错误:

Cannot make a static reference to the non-static method fetchAllCategories() from the type DatabaseAdapter

我的代码如下:

在 EditTask 中:

private void fillData() {
        Cursor categoriesCursor;
        Spinner categoriesSpinner = (Spinner) findViewById(R.id.spinDept);
        categoriesCursor = DatabaseAdapter.fetchAllCategories();

        startManagingCursor(categoriesCursor);

        String[] from = new String[] { DatabaseAdapter.CAT_NAME };

        int[] to = new int[] { R.id.tvDBViewRow }; //this part hasnt been implemented in to the layout yet

        SimpleCursorAdapter categoriesAdapter = new SimpleCursorAdapter(this,
                R.layout.db_view_row, categoriesCursor, from, to);

        categoriesSpinner.setAdapter(categoriesAdapter);
    }

在我的 DatabaseAdapter 类中,我有以下内容:

public Cursor fetchAllCategories() {
    if (mDb == null) {
        this.open();
    }
    String tableName = "CAT_TABLE";
    return mDb.query(tableName, new String[] { CAT_ID, CAT_NAME }, null,
            null, null, null, null);
}

有问题的代码行是:

categoriesCursor = DatabaseAdapter.fetchAllCategories();

我对 Java/Android 很陌生,所以它可能很简单/显而易见,但非常感谢任何帮助!

I have followed a tutorial to populate data from an sqlite database in to a spinner (dropdown) in Android. However, I am getting the error:

Cannot make a static reference to the non-static method fetchAllCategories() from the type DatabaseAdapter

My code is as follows:

In EditTask:

private void fillData() {
        Cursor categoriesCursor;
        Spinner categoriesSpinner = (Spinner) findViewById(R.id.spinDept);
        categoriesCursor = DatabaseAdapter.fetchAllCategories();

        startManagingCursor(categoriesCursor);

        String[] from = new String[] { DatabaseAdapter.CAT_NAME };

        int[] to = new int[] { R.id.tvDBViewRow }; //this part hasnt been implemented in to the layout yet

        SimpleCursorAdapter categoriesAdapter = new SimpleCursorAdapter(this,
                R.layout.db_view_row, categoriesCursor, from, to);

        categoriesSpinner.setAdapter(categoriesAdapter);
    }

And in my DatabaseAdapter class I have the following:

public Cursor fetchAllCategories() {
    if (mDb == null) {
        this.open();
    }
    String tableName = "CAT_TABLE";
    return mDb.query(tableName, new String[] { CAT_ID, CAT_NAME }, null,
            null, null, null, null);
}

The offending line of code is:

categoriesCursor = DatabaseAdapter.fetchAllCategories();

I'm pretty new to Java/Android so it may be something simple/obvious but any help is much appreciated!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春花秋月 2024-10-28 11:59:56

您必须首先实例化 DatabaseAdapter 对象。
例如:

DatabaseAdapter myDbAdapter = new DatabaseAdapter();
categoriesCursor = myDbAdapter.fetchAllCategories();

You have to first instantiate a DatabaseAdapter object.
eg:

DatabaseAdapter myDbAdapter = new DatabaseAdapter();
categoriesCursor = myDbAdapter.fetchAllCategories();
豆芽 2024-10-28 11:59:56

为了能够使您的代码正常工作,您需要将方法 fetchAllCategoires() 声明为 static,如下所示:

public static Cursor fetchAllCategories() 

因为您尚未实例化 DatabaseAdapter 对象,所以您不能仅通过类名引用调用它的方法之一,除非出现关键字 static在方法声明中。

to be able to make your code work, you need to declare the method fetchAllCategoires() as static as follows:

public static Cursor fetchAllCategories() 

Because you have not instantiated a DatabaseAdapter object, you cannot just call one of it's methods by a class name reference unless the keyword static appears in the method declaration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文