Jemshit
Jemshit

Reputation: 10038

Android RecyclerView inside Fragment is not updating Loader content

I have Fragment and it uses RecyclerView Adapter with LoaderManager to list items. RecyclerView i use is from this address. I've implemented LoaderManager in Fragment and gave context of its Activity to RecyclerView.

When i add new item to list, it does not refresh and show new item in list. I use same Adapter and structure on another activity and it works good. But here i use RecyclerView with Loader inside Fragment and it does not refresh list when i add item, instead i have to go back(finish fragment) and enter again.

I have 2 tables like this. First I get cursor from student_course table and from that cursor i get student ids of specific class. Then using data of first cursor, i get second cursor from students table which has list of students from same class. Then i send second cursor to adapter. I do below operation on both onCreateView() and onCreateLoader:

String[] projection0 = {
            DatabaseOpenHelper.COL_STUDENT_ID,
            DatabaseOpenHelper.COL_COURSE_ID};
    String selection0=DatabaseOpenHelper.COL_COURSE_ID + "=?";
    String selectionArgs0[]={String.valueOf(courseId)};
    mDataset0=getActivity().getContentResolver().query(StudentContentProvider.CONTENT_URI2, projection0,selection0,selectionArgs0,null);


    if(mDataset0.getCount()!=0) {
        //BATCH
        List<String> selectionList = new ArrayList<String>();
        mDataset0.moveToFirst();

        ...
        String[] projection1 = {
                DatabaseOpenHelper.COLUMN_ID,
                DatabaseOpenHelper.COL_STUDENT_NUMBER,
                DatabaseOpenHelper.COL_STUDENT_NAME,
                DatabaseOpenHelper.COL_STUDENT_CARD_ID,
                DatabaseOpenHelper.COL_STUDENT_COLOR};
        selection1 = selection1.substring(0, selection1.length() - 2) + ")";
        mDataset1 = getActivity().getContentResolver().query(StudentContentProvider.CONTENT_URI1, projection1, selection1, selectionArgs1, null);

    }else{
        mDataset1=null;
    }
...
mAdapter = new studentListAdapter(getActivity(),mDataset1,this);

mDataset1 is students of same class.

1) What might be the problem ? 2) Is there any way to implement LoaderManager class inside

Update 1: When i change selection1 and selectionArgs1 to null, it updates list immediately after i add item. But it shows all students from every class because i didn't specify selection.

Update 2: When i check onLoadFinished() i see that new cursor, after loading new content, is still same as old.

Upvotes: 1

Views: 1311

Answers (1)

Jemshit
Jemshit

Reputation: 10038

1) I realized that cursor is not changing when new data is added, so when i add new data i call getLoaderManager().restartLoader(LOADER_ID,null,this) (by localBrodCastManager in my case) which reloads cursor (calls onCreateLoader()) and new data is shown on list. (as Suggested here). This does trick.

2) I found that the reason cursor does not change when item is added, because on onCreateLoader() i returned cursor with specific id selection of students, which means cursor will not change until there is change on that items with specific ids i have given. So, if there is new item added, my cursoris not changed, but if one of the items(which is listed) is removed or changed, then cursor is changed. Solution is here.

Upvotes: 1

Related Questions