last day (14 days later) » 

4:06 AM
1
A: Sqlite error: Only the original thread that created a view hierarchy can touch its views

George MulliganYou should only access UI components such as views from the UI thread. As you have found if you try to access them from a background thread you will get an error. You can read the values from the database on the background thread but you should populate and set the adapter on the UI thread. If y...

 
Can the answer be changed to use getActivity().runOnUiThread correctly?
 
I added that as an option. All the methods mentioned really just post a Runnable to a queue to be run on the UI thread.
 
Sorry I mean in the code. Is the correct way just to replace mRecyclerView.post with getActivity().runOnUiThread and everything else is the same?
 
Yes everything else should be the same. runOnUiThread() also just takes a Runnable.
 
I assume that this thread will (potentially) remain running even if the function ends first, right? Is there a way to force it to wait for the threads to complete before ending the function? Or maybe that defeats the purpose
 
4:06 AM
Forcing it to wait for the thread to complete before returning from the function would be equivalent to blocking the UI thread so you would not want to do that. Why do you want to do that? There is likely a better way to achieve your goal.
 
I display a Snackbar text after the refresh is complete -- it is theoretically possible that the text would display even though the thread has not finished refreshing yet. So I'd have to make the Snackbar wait to display only when the refresh thread has finished. This refresh method is used in other areas so I can't just put the Snackbar inside the Ui run() portion. But maybe that is outside the scope of this question.
 
To do that just add it to the runnable's run() method after mRecyclerView.setAdapter(mRecyclerAdapter);. You can call a method to show the SnackBar so it is reusable in the other areas of your code still.
 
Right, but then that Snackbar would always display every single refresh whereas I am only trying to invoke it in certain situations (right now I display it after the call to refreshRecyclerView() in said situation)
 
Chat was getting a bit long in the comments
so you do not want to show the SnackBar on every call to refreshRecyclerView?
 
In my case, it has to do with a clear button. You press clear and it erases the values the user has inputted into the RecyclerView, so it clears the values, refreshes the RecyclerView, and then displays a Snackbar with something like "All values cleared"
I would not be displaying "All values cleared" every time the refresh method is called (since it is refreshed in other situations that don't involve clearing)
 
4:08 AM
One simple solution might be to pass a boolean flag to refreshMyRecyclerView() on whether or not to show the SnackBar once complete.
 
I would be fine with that; just wasn't sure what the accepted practice here was
 
If you do that you will have to make the boolean method parameter final since it is used in an anonymous class.
Not sure if you ran into that before or not so just an FYI.
 
I have, but thank you for the heads up
I am new to threads and so this answer was very helpful, thank you
 
👍🏼
You're welcome.
Have a good day.
 
You as well
 

last day (14 days later) »