使用 activeAndroid 和 SearchView 检索 CursorAdapter
Retrieve CursorAdapter with activeAndroid and SearchView
我尝试在我的工具栏中实现一个 "android.support.v7.widget.SearchView",它应该向用户提供一些建议。
我希望 activeAndroid 可以为我提供一种直接从我的查询中检索 CursorAdapter 的方法(基本上是一个 getAll())。
following link 似乎已被弃用,因为 .toSql() 需要私有访问并且 "Cache" 未解决。
有什么想法吗?
您可以使用以下方法通过 ActiveAndroid 创建 Cursor:
Cursor cursor = ActiveAndroid.getDatabase().rawQuery("SELECT * FROM TABLE", null);
您需要自己构建 CursorAdapter,但它非常简单,您提供的 link 的 "Defining the Adapter" 部分应该会为您提供入门所需的内容。
请注意 ActiveAndroid 3.1.0 确实将 .toSql() 显示为 public。
您需要做的一件事是确保您的 ActiveAndroid 数据库模型包含预期的 _id 列,该列在 ActiveAndroid 中默认不存在。您需要卸载该应用程序或执行数据库迁移以查看对基础数据库模型的更改。否则你可能会得到这个错误
java.lang.IllegalArgumentException: column '_id' does not exist
包括 ActiveAndroid 默认情况下不存在的预期“_id”列:
@Table(name = "Items", id = BaseColumns._ID)
像这样请求光标:
public Cursor getCursor() {
String sql = new Select()
.from(Item.class)
.toSql();
String[] params = null;
Cursor cursor = Cache.openDatabase().rawQuery(sql, params);
return cursor;
}
然后您可以像这样创建一个适配器:
ListAdapter adapter = new SimpleCursorAdapter(context,
android.R.layout.simple_list_item_1,
c, new String[] {"Name"}, new int[] { android.R.id.text1}
);
我尝试在我的工具栏中实现一个 "android.support.v7.widget.SearchView",它应该向用户提供一些建议。
我希望 activeAndroid 可以为我提供一种直接从我的查询中检索 CursorAdapter 的方法(基本上是一个 getAll())。
following link 似乎已被弃用,因为 .toSql() 需要私有访问并且 "Cache" 未解决。
有什么想法吗?
您可以使用以下方法通过 ActiveAndroid 创建 Cursor:
Cursor cursor = ActiveAndroid.getDatabase().rawQuery("SELECT * FROM TABLE", null);
您需要自己构建 CursorAdapter,但它非常简单,您提供的 link 的 "Defining the Adapter" 部分应该会为您提供入门所需的内容。
请注意 ActiveAndroid 3.1.0 确实将 .toSql() 显示为 public。
您需要做的一件事是确保您的 ActiveAndroid 数据库模型包含预期的 _id 列,该列在 ActiveAndroid 中默认不存在。您需要卸载该应用程序或执行数据库迁移以查看对基础数据库模型的更改。否则你可能会得到这个错误
java.lang.IllegalArgumentException: column '_id' does not exist
包括 ActiveAndroid 默认情况下不存在的预期“_id”列:
@Table(name = "Items", id = BaseColumns._ID)
像这样请求光标:
public Cursor getCursor() {
String sql = new Select()
.from(Item.class)
.toSql();
String[] params = null;
Cursor cursor = Cache.openDatabase().rawQuery(sql, params);
return cursor;
}
然后您可以像这样创建一个适配器:
ListAdapter adapter = new SimpleCursorAdapter(context,
android.R.layout.simple_list_item_1,
c, new String[] {"Name"}, new int[] { android.R.id.text1}
);