Ben Edwards
10/14/2022, 11:04 PMBen Edwards
10/15/2022, 12:32 AMprivate val viewModel by lazy { ViewModelProvider(this)[TaskTimerViewModel::class.java] }
private val mAdapter = CursorRecyclerViewAdapter(null) // null=view with instructions
In onCreate set up the observer
viewModel.cursor.observe( // New
this, Observer { cursor -> mAdapter.swapCursor(cursor)?.close() }
)
and in onViewCreated setup the adapter
binding.taskList.adapter = mAdapter // Attach Adapter to Recyclerview
Looking at Logcat when I query the DB in AppPrivider I get two rows returned then later on I see the CursorRecyclerView adapter has no records. This Logcat is from just running the App without interacting with it. I am expecting to see the records from the database but I just see the instructions (which are displayed when there is no data).
AppProviderXX com.funkytwig.takstimer D onCreate
MainActivityXX com.funkytwig.takstimer D onCreate
MainFragmentXX com.funkytwig.takstimer D onAttach
MainFragmentXX com.funkytwig.takstimer D onCreate
TaskTimerViewModelXX com.funkytwig.takstimer D init
TaskTimerViewModelXX com.funkytwig.takstimer D loadTasks
AppProviderXX com.funkytwig.takstimer D query for uri=<content://com.funkytwig.tasktimer.provider/Tasks>
AppProviderXX com.funkytwig.takstimer D buildUriMatcher
AppProviderXX com.funkytwig.takstimer D query: 2 rows returned **********
TaskTimerViewModelXX com.funkytwig.takstimer D loadTasks done
MainFragmentXX com.funkytwig.takstimer D onCreate done
MainActivityXX com.funkytwig.takstimer D onCreate done
MainActivityXX com.funkytwig.takstimer D onStart
MainFragmentXX com.funkytwig.takstimer D onCreateView
MainFragmentXX com.funkytwig.takstimer D onViewCreated
MainFragmentXX com.funkytwig.takstimer D onViewStateRestored
MainFragmentXX com.funkytwig.takstimer D onStart
MainActivityXX com.funkytwig.takstimer D onResume
MainFragmentXX com.funkytwig.takstimer D onResume
CurRecViewAdapterXX com.funkytwig.takstimer D getItemCount: no items so return 1 **************
CurRecViewAdapterXX com.funkytwig.takstimer D swapCursor new & previous cursor unchanged
CurRecViewAdapterXX com.funkytwig.takstimer D getItemCount: no items so return 1
CurRecViewAdapterXX com.funkytwig.takstimer D getItemCount: no items so return 1
CurRecViewAdapterXX com.funkytwig.takstimer D onCreateViewHolder
CurRecViewAdapterXX com.funkytwig.takstimer D onBindViewHolder: cursor empty
MainActivityXX com.funkytwig.takstimer D onCreateOptionsMenu
Full code at https://github.com/funkytwig/tasktimer/tree/master/app/src/main but I have included what I think are the most relevant classes below.
I have also posted a longer more detailed version of this question at https://stackoverflow.com/questions/74071324/problem-with-adapter-on-cursor-recyclervew but have had no responses.Chrimaeon
10/15/2022, 12:35 AMswapCursor
method you are not assigning the newCursor
to this.cursor
Ben Edwards
10/15/2022, 2:44 PMfun swapCursor(newCursor: Cursor?): Cursor? {
val func = "swapCursor"
if (newCursor === cursor) return null
val numItems = itemCount
val oldCursor = cursor
if (newCursor != null) {
Log.d(TAG, "$func new & previous cursor unchanged")
this.cursor = cursor // *******
// notify observer about cursor
notifyDataSetChanged()
} else { // cursor has changed
Log.d(TAG, "$func new & previous cursor different")
// Notify observer about lack of dataset, all of it from 0 to newItems,
// i.e. whole range of records has gone
notifyItemRangeChanged(0, numItems)
}
return oldCursor
}
ut no joy. Is that what you mean?Chrimaeon
10/15/2022, 2:55 PM...
if (newCursor != null) {
this.cursor = newCursor
notifyDataSetChanged()
}
...
so then you/the adapter has access to the new cursor in all the other methods and the Recyclerview can start from the beginning with creating your views.Chrimaeon
10/15/2022, 2:57 PMBen Edwards
10/16/2022, 6:44 PMBen Edwards
10/16/2022, 8:11 PMcursor = newCursor
at the beginning but when I put it in it made a lot more sense. Thanks your help. now you have pointed me in the correct direction. It now makes a lot more sence.