Office Hours — Today, June 4

Thursday, May 30

Jun 4
9:55 AM
Mark M.
has entered the room
Mark M.
turned on guest access
10:40 AM
Sherman
has entered the room
Mark M.
hello, Sherman'
how can I help you today?
Sherman
Hi Mark
I am trying to fill a view with information from a database with information from an asynctask and i am having some trouble
it seems that by the time the asynctask comes back with information the view has already rendered
and when i try to get the information from the variable it always null
10:45 AM
Mark M.
"the view has already rendered" is what you want
that's the entire point behind using a background thread, so the main application thread can render the UI
use onPostExecute() of the AsyncTask to update the UI
Sherman
okay so what do i call in the asynctask to update the ui?
Mark M.
you call Java code
the same sort of code you would use to update the UI anywhere else
Sherman
is there a specific method that will do that?
Mark M.
there are hundreds of thousands of methods in Android that update the UI, such as setText() on a TextView
Sherman
i am executing the async task from a DatabaseHelper object
am i supposed to be doing UI updates from an onPostExecute from there?
Mark M.
you need to be doing something to cause the UI to update from there
whether onPostExecute() directly does the UI updates, or delegates to something else, is up to you
Sherman
if i wanted to refresh a ListView what would I call?
Mark M.
for example, onPostExecute() could trigger an event that causes the activity to know to update the UI, such as via LocalBroadcastManager, or an event bus like Otto
10:50 AM
Mark M.
since I have no idea what ListView you are talking about or how it was set up, I cannot answer that
Sherman
i was looking at your EmPubLite example where you do this through the listener method on the view
it is called setNote
Mark M.
definitely not on the view
Sherman
okay...
Mark M.
that's a method, defined on the NoteListener interface, implemented on NoteFragment
I pass an instance of the listener to getNoteAsync()
the AsyncTask calls setNote() in onPostExecute()
Sherman
right.
i was trying to adopt that to what i am trying to do
but i am using a BaseAdapter to combine information from my DB and my View
Mark M.
so?
10:55 AM
Sherman
i don't know how to tell the adapter to update the ui
Mark M.
call notifyDataSetChanged(), which will cause the ListView to re-load all its rows from the adapter
Sherman
would I call that from setNote()?
(if i was to use your example)
Mark M.
yes
Sherman
it is telling me that the method doesn't exist
so i am missing something
Mark M.
notifyDataSetChanged() is a method on BaseAdapter, as you can tell by looking at the JavaDocs: http://developer.android.com/reference/android/...
11:00 AM
Sherman
so i would define the BaseAdapter as a class variable and set it before the call and then call notiifyDataSetChanged() on it in the setNote() method?
before the asynctask call I mean
Mark M.
I have no idea what you consider a "class variable" to be, as there is nothing by that term in Java
if you mean "a data member of the object that has the setNote() equivalent", then yes
if you mean "a *static* data member", then no
Sherman
uhh... what is a variable that all methods in a class can see?
Mark M.
a data member
Sherman
okay
sorry
Mark M.
for example, setNote() calls setText() on a TextView that is held as a data member of that fragment
Sherman
and having a BaseAdapter as a data member of that fragment is one way I could do this?
Mark M.
yes
Sherman
okay thank you for your help
Mark M.
that's a wrap for today's chat
next one is Thursday at 4pm Eastern
Sherman
cool.
Mark M.
this chat's transcript will be archived at http://commonsware.com/office-hours/ shortly
have a pleasant day!
Sherman
has left the room
Mark M.
turned off guest access

Thursday, May 30

 

Office Hours

People in this transcript

  • Mark Murphy
  • Sherman