Android的CursorLoader有选择和selectionArgs两个[] [英] Android CursorLoader with selection and selectionArgs[]

查看:317
本文介绍了Android的CursorLoader有选择和selectionArgs两个[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用装载机 RecyclerView.Adapter 列表项。我想列出从数据库表中特定项目。所以,我所做的:

I am using Loader for RecyclerView.Adapter to list items. I want to list specific items from database table. So i did:

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 selectionArgs两个但在这里,我列出具体标识项目。当新的项目添加到表,我想列出这问题就出现了。在光标我是回不知道新的项目,因为我给了3特定项目,所以它只是检测时,有变化的那些3项。

如何列出当被加入了一些 ID 新项目,我想列出来呢?我应该从项目数据库中的所有项目 RecyclerView.Adapter onBindViewHolder过滤标识()

Normally i give null,null to selection and selectionArgs but here, i list items with specific IDs. Problem arises when new item is added to table and i want to list it. The cursor i am returning is not aware of new item since i gave 3 specific items, so it just detects when there is change on those 3 items.

How to list when there is new item added with some ID and i want to list it too ? Should i project all items from database to RecyclerView.Adapter and filter IDs in onBindViewHolder()?

推荐答案

既然我已经限制标识光标 ,如果只有该特定项改变它被改变,而不是当添加新的项目。我做了 onLoadFinished()一招,创造新的光标和交换新的光标。因此,当有变化,我得到我的选择新的游标 selectionArgs两个又说:

Since I have restricted IDs in cursor, it is changed if only that specific items change, not when new item is added. I did a trick on onLoadFinished() to create new cursor and swap that new cursor. So when there is change, I get new cursor with my selection and selectionArgs again:

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;
      }
}

这篇关于Android的CursorLoader有选择和selectionArgs两个[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆