2

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

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()?

Jemshit
  • 9,501
  • 5
  • 69
  • 106
  • you need a "notyfying" custom content provider, see ContentResolver.notifyChange() usage when implementing a content provider – pskink Mar 21 '15 at 10:19
  • @pskink yes i have that, it notifies, but my cursor is not changed since i have restricted `IDs` in cursor. Cursor is changed if only that specific items change, not when new item is added. – Jemshit Mar 21 '15 at 10:37
  • So call notifyChange when inserting new rows as well... – pskink Mar 21 '15 at 10:39
  • @pskink it is called, problem is `cursor` is not listing all items, so it does not change list unless i add new `IDs` in selection again. Because `cursor` is not aware of new items even it is notified, since i restricted `cursor` with some `IDs` – Jemshit Mar 21 '15 at 10:46

1 Answers1

2

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;
      }
}
Jemshit
  • 9,501
  • 5
  • 69
  • 106
  • I dont understand why you need some specific ids and then you are surprised that your loader doesn't see new ids, so use selection based on other fields, not ids – pskink Mar 21 '15 at 10:53
  • @pskink i can't, because my database design is as like here: http://stackoverflow.com/questions/28946355/database-choice-for-creating-two-connected-tables?lq=1 . I have 2 tables, first i get `student IDs` from `student_course` table (all students of same class) and then i display students with that `student IDs` – Jemshit Mar 21 '15 at 11:33
  • @pskink if you have suggestion i want to hear – Jemshit Mar 21 '15 at 11:42
  • What students should your loader retrieve? – pskink Mar 21 '15 at 11:44
  • @pskink Students from specific class(math..). There are many classes and many students. One student may have to classes. – Jemshit Mar 21 '15 at 12:04
  • So add a third table, see http://en.m.wikipedia.org/wiki/Many-to-many_(data_model) – pskink Mar 21 '15 at 12:09
  • i have 3rd table, i don't have problem with that. You asked to "use selection based on other fields", which i can not with this table design. – Jemshit Mar 21 '15 at 12:12
  • So add classId in the third table and select only rows that have that classId – pskink Mar 21 '15 at 12:14