Jun 4 | 9:55 AM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Jun 4 | 10:40 AM |
Sherman | has entered the room |
Mark M. |
hello, Sherman'
|
Mark M. |
how can I help you today?
|
Sherman |
Hi Mark
|
Sherman |
I am trying to fill a view with information from a database with information from an asynctask and i am having some trouble
|
Sherman |
it seems that by the time the asynctask comes back with information the view has already rendered
|
Sherman |
and when i try to get the information from the variable it always null
|
Jun 4 | 10:45 AM |
Mark M. |
"the view has already rendered" is what you want
|
Mark M. |
that's the entire point behind using a background thread, so the main application thread can render the UI
|
Mark M. |
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
|
Mark M. |
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
|
Sherman |
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
|
Mark M. |
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
|
Jun 4 | 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
|
Sherman |
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
|
Mark M. |
I pass an instance of the listener to getNoteAsync()
|
Mark M. |
the AsyncTask calls setNote() in onPostExecute()
|
Sherman |
right.
|
Sherman |
i was trying to adopt that to what i am trying to do
|
Sherman |
but i am using a BaseAdapter to combine information from my DB and my View
|
Mark M. |
so?
|
Jun 4 | 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()?
|
Sherman |
(if i was to use your example)
|
Mark M. |
yes
|
Sherman |
it is telling me that the method doesn't exist
|
Sherman |
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/...
|
Jun 4 | 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?
|
Sherman |
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
|
Mark M. |
if you mean "a data member of the object that has the setNote() equivalent", then yes
|
Mark M. |
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
|
Sherman |
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
|
Mark M. |
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
|
Mark M. |
have a pleasant day!
|
Sherman | has left the room |
Mark M. | turned off guest access |