CursorAdapter 无法实例化!
我有一个 DataBaseHelper,它有一个 fetchData() 方法,该方法返回一个 Cursor,其中包含资产文件夹中 .sqlite 文件中的整个表行。
我这样写:
try {
Cursor cursor = myDbHelper.fetchData("tableName");
txt.setText("Done!"); //for check only
} catch (Exception e){
txt.setText("Can't fetch data !");
}
我在 try 块的末尾添加了这两行。我这样做是为了将 CursorAdapter ca
附加到 ListView lv
:
CursorAdapter ca = new CursorAdapter(getBaseContext(), cursor, false);
lv.setAdapter(ca);
但是,我收到一条错误消息在 CursorAdapter 实例化行:
无法实例化 CursorAdapter 类型
这里出了什么问题?我是否使用了错误的适配器从数据库中获取数据?
I have a DataBaseHelper which has a fetchData()
method that returns a Cursor
containing a whole table rows drom an .sqlite file in assest folder.
I wrote this:
try {
Cursor cursor = myDbHelper.fetchData("tableName");
txt.setText("Done!"); //for check only
} catch (Exception e){
txt.setText("Can't fetch data !");
}
I added these two lines at the end of the try block. I did this in order to attach a CursorAdapter ca
to a ListView lv
:
CursorAdapter ca = new CursorAdapter(getBaseContext(), cursor, false);
lv.setAdapter(ca);
However, I got an error message at the CursorAdapter instantiation line:
Cannot instantiate the type CursorAdapter
What is wrong here ? am I using the wrong adapter to fetch from a database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯......让我们看看:
首先我们打开 CursorAdapter 的参考页面。
然后我们查看标题,我们可以看到:
公共抽象类CursorAdapter
抽象类不能在 Java 中实例化(以及在任何其他实现该概念的语言中),这是一个功能,而不是一个错误。
再几行,我们可以找到 SimpleCursorAdapter 的链接,它不是抽象的。
所以简单的解决方案是“只需使用 SimpleCustomAdapter 而不是抽象 CursorAdapter。SimpleCursorAdapter
自 Android 1.0(API lvl1)以来就存在于 Android API 中。
Hmm.... let's see:
First we open reference page for CursorAdapter.
Then we look at header, and we can read:
public abstract class CursorAdapter
Abstract classes can't be instantiated in Java (and in any other language implementing that concept) it's feature not a bug.
Few lines more we can find link to SimpleCursorAdapter which is not abstract.
So simple solution is "just use SimpleCustomAdapter instead of abstract CursorAdapter.
SimpleCursorAdapter exists in Android API since Android 1.0 (API lvl1).