- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 RecyclerView.Adapter
使用 Loader
来列出项目。我想列出数据库表中的特定项目。所以我做了:
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String selectionArgs1[]={"1","13","14"};
String selection1 = DatabaseOpenHelper.COLUMN_ID + " in (";
for (int i = 0; i < selectionArgs1.length; i++) {
selection1 += "?, ";
}
selection1 = selection1.substring(0, selection1.length() - 2) + ")";
String[] projection1 =...
return new CursorLoader(getActivity(),StudentContentProvider.CONTENT_URI1, projection1, selection1,selectionArgs1, null);
}
通常我将 null,null
赋给 selection
和 selectionArgs
但在这里,我列出了具有特定 ID
的项目.当新项目添加到表中并且我想列出它时出现问题。我返回的 cursor
不知道新项目,因为我给了 3 个特定项目,所以它只是检测这 3 个项目何时发生变化。
当有添加了一些 ID
的新项目并且我也想列出它时如何列出?我应该将所有项目从数据库投影到 RecyclerView.Adapter
并在 onBindViewHolder()
中过滤 ID
吗?
最佳答案
因为我在 cursor
中限制了 IDs
,所以只有当特定项目发生变化时它才会改变,而不是在添加新项目时。我在 onLoadFinished()
上做了一个技巧来创建新的 cursor
并交换那个新的光标。因此,当发生变化时,我再次使用 selection
和 selectionArgs
获得新光标:
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
switch (loader.getId()) {
case LOADER_ID:{
String selectionArgs1[]={...};
String selection1 = DatabaseOpenHelper.COLUMN_ID + " in (";
for (int i = 0; i < selectionArgs1.length; i++) {
selection1 += "?, ";
}
selection1 = selection1.substring(0, selection1.length() - 2) + ")";
String[] projection1 =...
mDataset1 = getActivity().getContentResolver().query(StudentContentProvider.CONTENT_URI1, projection1, selection1, selectionArgs1, null);
mAdapter.swapCursor(mDataset1);
break;
}
}
关于带有选择和 selectionArgs[] 的 Android CursorLoader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29181308/
我在 stack overflow 中回答了一个问题,提出这个问题的用户问我: SQlite Query in android using cursor why i must give my sele
我正在尝试创建一个游标加载器,我想在其中查询两个项目的特定列,类似于: SELECT * FROM TABLE_NAME WHERE COLUMN_NAME IN(something, somethi
我正在尝试使用以下内容: String dbquery = "Select * From stops where stopname LIKE '%?%' LIMIT 100"; String[] ui
我有一个包含 BLOB 列的表(它只有 16 个字节)。我想运行 SELECT * FROM myTable WHERE blobColumn = ? 形式的查询并将字节数组绑定(bind)到该列。理
我想使用选择查询从表中检索数据。我发现,SQLiteDatabase 类的 rawQuery(query, selectionArgs) 方法可以检索数据。但是我不知道 query 和 selecti
这个问题已经有答案了: PreparedStatement IN clause alternatives? (33 个回答) 已关闭 8 年前。 我遇到过这个问题: Caused by: java.l
我正在尝试显示特定文件夹中的音频文件。文件夹由用户选择并添加到 arraylist,因此 arraylist 是动态的。现在,当我查询多个文件夹时,mediaStore 不会返回任何结果。我认为问题出
我正在为 RecyclerView.Adapter 使用 Loader 来列出项目。我想列出数据库表中的特定项目。所以我做了: public Loader onCreateLoader(int id,
我有以下内容解析器查询代码: String selection = Columns.NAME + "=? and " + Columns.AVAILABILITY + "=?"; String[] s
我正在尝试从数据库中选择一些数据,我有两段代码可以做到这一点: cursor = builder.query(db, new String[]{"col1",
正如Android文档所说,rawQuery方法的selectionArgs参数被解析为字符串。 SQLiteDatabase.rawQuery(String sql, String[] select
当我在查询中使用 selectionArgs 时,应用程序崩溃日志 12-30 22:22:05.406: ERROR/AndroidRuntime(532): FATAL EXCEPTION: ma
我有如下表格: 同学们: id | student_name ------------------ 1 | max 2 | alex 3 | james 我只想查询 ID
似乎 selectionArgs 没有正确绑定(bind),但我在这里找不到任何错误。 这将返回 2 个结果和那个是正确的 String sql = "SELECT * FROM tblEvent_e
我在 Android 上使用 SQLite。 我使用 ContentProvider 从数据库中查询数据。现在,当我尝试通过 ContentResolver 使用子查询时遇到问题 String sel
在 android 中,如果我像下面这样调用 SQLiteDatabase 方法查询: sqlitedb.query("mytable", null, "name = ?", new Str
如何在 SQLiteDatabase.query() 中使用 String[] selectionArgs?我希望我可以将它设置为 null,因为我没有用它。我只是想将整个未排序的表从数据库加载到 C
我想选择查询以从具有两个条件的一个表中检索数据我的代码是: Cursor res = mDatabase.rawQuery("select * from " + tableName + " where
我正在实现一个 ContentProvider,当我实现查询方法时,我遇到了一些困难。 public final Cursor query (Uri uri, String[] projection,
我在旧设备上使用非官方 API 从 Stock Google Calendar 获取事件。现在我正在重构代码以使用 android.content.ContentResolver.query() 的
我是一名优秀的程序员,十分优秀!