Office Hours — Today, April 25

Yesterday, April 24

Apr 25
7:25 PM
Mark M.
has entered the room
Mark M.
turned on guest access
7:30 PM
wuqiang
has entered the room
wuqiang
Hi , morning
In china now is morning
..... good afternoon
Mark M.
hello, wuqiang!
how can I help you today?
wuqiang
View paste (16 more lines)
 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        new Thread(new Runnable() {
            @Override
            public void run() {
                // Fetch data from website
                BmobQuery<Restaurant> query = new BmobQuery<Restaurant>();
                query.setLimit(10);
                query.findObjects(new FindListener<Restaurant>() {
                    @Override
                    public void done(List<Restaurant> object, BmobException e) {
                        if (e == null) {
                            // Success
                            for (Restaurant restaurant : object) {
...
7:35 PM
Mark M.
let me guess: you are having problems with notifyDataSetChanged()
wuqiang
View paste
This is a snippet of code  fetch data from website ,I want it work  in another Thread ?
and I  want the Adapter began to  work only after the data have fetched ,Am i doing it the right way?
Mark M.
you cannot call notifyDataSetChanged() on a background thread
at least, assuming that restaurantAdapter is a ListAdapter of some form
so, that little bit of work needs to be done on the main application thread
there are many options for this
I outline them in my chapter on threads
wuqiang
it is a RecyclerView's Adapter
Mark M.
I have not tried calling notifyDataSetChanged() on a RecyclerView.Adapter
(on a background thread, that is)
I would expect it to have the same problems as would ListView and a ListAdapter
with a crash saying that you are modifying the views from a thread other than the thread that created them
but, perhaps they added something to RecyclerView that handles this issue for you
I do not know much about BmobQuery, BmobException, etc., so I do not know if you have any problems with that
7:40 PM
Mark M.
you are modifying the restaurants collection from a background thread, and presumably you are accessing it from the main application thread, so you would need to use some sort of synchronization there
or, if you wind up moving notifyDataSetChanged() to the main application thread, update the restaurants collection on the main application thread as well
wuqiang
and after annotating "restaurantAdapter.notifyDataSetChanged();" ,it doesn't work
have noting to show
and where should i put restaurantAdapter.notifyDataSetChanged(); in main Thread ?
Mark M.
options for moving that code to the main application thread include: switching from Thread to AsyncTask, using post() on some view, using runOnUiThread() on your activity, and using an event bus
I cover all of those in my chapter on threads in the book
(pages 523-547 of Version 8.5 of the book)
7:45 PM
wuqiang
thanks
let me have a look
and i have no question today
Thands a lot
Thanks a lot
Mark M.
happy to help!
8:20 PM
Rick
has entered the room
8:25 PM
Mark M.
hello, Rick!
do you have a quick question?
Rick
Hi! I did but my 3 year old is screaming. I'll have to try again next time.
Mark M.
OK
Rick
Thanks though.
Mark M.
tell your 3-year-old that I said "shhh -- it'll be OK" :-)
Rick
He doesn't know "shhh"
Next office hour's when?
Mark M.
Saturday at 4pm US Eastern
Rick
okay, thank you
8:30 PM
Mark M.
that's a wrap for today's chat
the transcript will go up on https://commonsware.com/office-hours/
have a pleasant day!
wuqiang
has left the room
Rick
has left the room
Mark M.
turned off guest access

Yesterday, April 24

 

Office Hours

People in this transcript

  • Mark Murphy
  • Rick
  • wuqiang