Office Hours — Today, September 23

Tuesday, September 21

Mark M.
has entered the room
Sep 23
7:55 PM
Mark M.
turned on guest access
8:00 PM
spargonaut
has entered the room
spargonaut
hey there
Mark M.
howdy, spargonaut!
spargonaut
great books btw. i've been devouring them
Mark M.
thanks! I find them to be best served with salsa!
:-)
spargonaut
nice
Mark M.
how can I help you today?
Mike R.
has entered the room
spargonaut
well, i've got a couple of questions actually
the first is...
Mike R.
Hello
spargonaut
i'm wanting to pass some custom objects from one activity to the next
Mark M.
urk
spargonaut
but I can't seem to figure out an easy / logical way to do that
Mark M.
(btw, hi, Mike!)
what is the nature of the "custom objects"?
spargonaut
hey mike!
thanks mark, i'll look this over and see if it explains what i'm looking for
Mark M.
uh, well, it's more that it explains why your question makes me nervous
8:05 PM
spargonaut
oh. heh
well, let me explain my app a little
the main activity make a request to an LBS and gets back an xml doc
i parse the xml doc into a custom object and put it into a hashmap
since i'm relying on an http reply, i've put the request into an extended async class
when it says its built all of the entries of the hashmap, i'm wanting a button that starts a listactivity of all the items in the hashmap
but i can't figure out how elegantly pass the hashmap from one activity to the next
Mark M.
to me, it feels like you are putting too many roles in the first activity
spargonaut
hrmm...
Mark M.
off the cuff, here's how I'd consider approaching it:
8:10 PM
Mark M.
have the LBS HTTP call, parsing, and resulting data be handled by something other than an activity
most likely a Service
spargonaut
k
Mark M.
have the first activity tell the service, "yo, go generate the results and let me know when you're done"
spargonaut
k
Mark M.
service does the work and lets the activity know by any number of possible means (callback/listener object, "narrowcast" broadcast Intent, createPendingResult(), etc.)
first activity then starts up second activity
second activity tells the service, "yo, give me the data"
spargonaut
ahh. alright
Mark M.
service returns the data, probably via an API exposed via onBind()
basically, you keep your model and your GUI separate
spargonaut
so i guess the service is sort of like a background thread
Mark M.
the service would use your existing AsyncTask, with minor mods probably
spargonaut
werd
Mark J.
has entered the room
Mark M.
there are other possible patterns here that some people use (e.g., custom Application object or static data member for holding the model data)
spargonaut
sweet. you've definitely given me stuff to chew on.
Mark M.
IMHO, any of those are better than trying to keep passing your model data around in Intent extras
OK, let me give the others a shot, and we'll swing back to you later for follow-up or additional questions
spargonaut
yea, i saw a bunch of those suggestions, but none of them seemed quite right
Mark M.
(btw, hi, Mark J!)
Mike: got a question?
Mark J.
Heya
spargonaut
yea, werd
Mike R.
sure do.
spargonaut
thx
Mike R.
last time we went over some stack traces i was getting
Mark M.
yup
Mike R.
specifically, crashing after a call to onActivityResult
8:15 PM
Mark M.
yup, and I theorized it might be tied to a rotation or something
Mike R.
you had guessed that there was a configuration change going on and some variables were not initialized
turns out you were right.
Mark M.
booya!
Mike R.
configuration changes / onActivityResult
Mark M.
well, ok, it sucks for your code
Mike R.
it seems to be an activity lifecycle problem
onCreate is definitely being called.
problem is that for most of my activities, the last line of onCreate is a bindService call
so what's happening is onCreate is called, then onActivityResult is called and then my onService method is called - in that order
Mark M.
by "my onService method", you mean onServiceConnected() in the ServiceConnection?
Mike R.
exactly
Mark M.
and the data member in question isn't being initialized until onServiceConnected()?
Mike R.
in most cases, onActivityResults sets some variables and re-builds the view.
correct again
so, i just test to see if my point to the service is null and if it is I set my variables and let onServiceConnected handle building the view.
Mark M.
seems reasonable
Mike R.
problem is, i have some onActivityResult methods that do a lot more complicated processing.
Mark M.
that's why mankind invented the [Delete] key
:-)
8:20 PM
Mike R.
so i've set a bunch of booleans and let onServiceConnected test the booleans and do that processing there if necessary
it works (i think), but it seems kind of a kludge
is there a more elegant, maintainable way to handle this activity lifecycle issue?
Mark M.
well, I don't know any way you can force onActivityResult() to occur after onServiceConnected()
I haven't tried passing a ServiceConnection between activities via onRetainNonConfigurationInstance(), so I don't know if that causes memory leaks
Mike R.
i've thought of trying that, but it scares me
is there a way to force bindService to be more synchronous?
Mark M.
a slightly simpler kludge might be to have onActivityResult() see if the connection is ready, and if not, use post() (on View or Handler) to delay execution of its work
no, bindService is very asynchronous
Mike R.
never used post(). where is it documented?
Mark M.
it's a method on View and on Handler
it takes a Runnable and sticks it on the queue that the main application thread works off of
actually, now that I think about it, that's probably not going to help
Mike R.
i'll take a look at it, but i may just stick to my booleans
Mark M.
yeah, that's probably your best short-term bet
Mike R.
thanks mark
8:25 PM
Mark M.
I do need to figure out if we can get a ServiceConnection to survive a configuration change, though
that strikes me as an important bit of info
I'll make a point to try to get an answer on that
Mike R.
yeah. let me know what you find out. i'll do some experimenting as well
Mark M.
Mark J: got a question?
Mark J.
View paste
Yeah, I know absolutely nothing about SQLite, but was just starting with it. My simple question is: is it possible setup a structure in a database like this:
-Category1
--Sub-Category1
--Sub-Category2
oopsie
Mark M.
um, could you explain a bit more about what you mean by that "structure in a database"?
Mark J.
Well, what I meant, Is it possible to store things multiple directories deep? Like the standard tree filesystem of most common filessystems
spargonaut
alright fellas, i'm outta here. thanks fore the help mark, i'll chat with you next time. good luck with your problems mike and other mark.
spargonaut
has left the room
Mark M.
typical way to do that in a relational database is to have a table that has a foreign key relationship back to itself, to identify the parent
for example, CREATE TABLE stuff (_ID INTEGER PRIMARY KEY AUTOINCREMENT, categoryName TEXT, parentId INTEGER)
parentId would be NULL for roots (e.g., Category1)
Mark J.
Because I started playing around in a client, and was having trouble breaking up the content, because I could not separate the content into sub categories very easily
Mark M.
a bit more common nowadays is to use a tagging system, but that does not usually enforce any sort of hierarchy
8:30 PM
Mark J.
Right, so what my wonder was, is it possible to structure something like system folders, breaking things up into directories 5+ levels deep?
Mark M.
sure
use the structure I just described
level5's parentId is level4
level4's parentId is level3
etc.
Mark J.
Alrighty
Mark M.
certain types of queries are rather painful this way, which I suspect is one of the reasons people tend to try something a bit more flat, like tags
Mark J.
Okay
My other question
Mark M.
um, hang on
Mark J.
What would the process be to build a database before hand, and then import it into a project?
alright
Mark M.
Mike: did you have a 2nd question?
Mike R.
no i'm good
Mark M.
(Mark -- just trying to be fair to all participants)
OK, back to Mark
Mark J.
Oh yeah, understandable
Mark M.
the process depends a bit on the size of the database
there are plenty of SQLite tools you can use from your desktop or browser to create a database
8:35 PM
Mark M.
I use SQLite Manager, an extension in Firefox
Mark J.
Right, and that is the route I am taking
Mark M.
but there are others
then, I'd put it either in assets/ or res/raw/ of the project
on first run, if you do not see your database there, copy it from the APK to the database directory using Java I/O, operating on the InputStream you get from the asset or raw resource
there's a size limit where this strategy fails (1MB, I think)
Mark J.
Aww really?
Mark M.
bigger than that, you need to cheat slightly and give your database some file extension that Android won't try compressing
.png or .mp3 might work
Mark J.
Okay, well once I have my database imported, how can I query it?
Mark M.
normally
using SQLiteDatabase
once it is in the regular filesystem, you use it like any "normal" SQLite database
SQLiteOpenHelper may or may not be terribly useful
haven't tried it in this scenario
Mark J.
Okay
Mark M.
also, bear in mind you'll be taking up extra on-board flash space -- some for the copy in the APK, some for the copy on the filesystem
depending on the database size, this may not make you very popular
Mark J.
:D
8:40 PM
Mark M.
BTW, before I forget, if either of you are Eclipse users, grab the new ADT plugin (0.9.9) if you are using the 0.9.8 version released earlier this month
apparently, the new version fixes some fairly significant bugs
Mike R.
has left the room
Mark M.
well, obviously Mike ran off to check his copy of Eclipse... :-)
any other questions?
Mark J.
Umm, not at the moment
Just grabbed the firefox plugin for SQLite :)
8:45 PM
Mark J.
Any good books or resources to look at for SQLite basics?
Mark M.
I have The Definitive Guide to SQLite (published by Apress)
at the time, at least, it was the only book out there
not bad, but it is best only for general SQLite stuff
it has nothing on Android's SQLite integration
the docs on sqlite.org usually cover most of my needs -- I haven't cracked that book open in a year or so
Mark J.
Alrighty
8:50 PM
Mark J.
Sweet, I just found some tutorials from GoogleTechTalks :)
8:55 PM
Mark M.
OK, any final questions?
Mark J.
No, But I will see you next time
Mark M.
yup, got two more scheduled for next ewek
er, week
ok, well, have a pleasant weekend!
Mark J.
Same to you
Mark J.
has left the room
Mark M.
turned off guest access

Tuesday, September 21

 

Office Hours

People in this transcript

  • Mark Johnston
  • Mark Murphy
  • Mike Renda
  • spargonaut