Office Hours — Today, July 11

Tuesday, July 9

Mark M.
has entered the room
Jul 11
7:25 PM
Mark M.
turned on guest access
jdub
has entered the room
Mark M.
hello, jdub!
how can I help you today?
jdub
Hi Mark
Well I am still having troubles with Horizontal List View
I looked at your example.
tunneling
has entered the room
jdub
I followed it, but nothing shows up when I use HorizontalListView
tunneling
How D
jdub
So I have a question
Mark M.
(hello, tunneling -- I will be with you shortly!)
jdub
The HorizontalListView also need to add children to its view
It does so through a setAdapter call normally
7:30 PM
jdub
so I somehow would have to perform to setAdapter calls, one for the ListView and one for the HorizontalListView
Mark M.
correct
EGHDK
has entered the room
Mark M.
the setAdapter() calls for the HorizontalListView would presumably be done in getView() of the adapter that you are using for the ListView
Yurii L.
has entered the room
jdub
Where should that second setAdapter call happen, in the getView
Mark M.
(hello, EGHDK -- be with you in a bit!)
jdub
Yeah I tried that but I get an empty list
Mark M.
(hello, Yurii -- you're in line after tunneling and EGHDK!)
jdub
arg
:-)
It looks like everything is getting called but nothing shows up.
tunneling
i don't have any questions
jdub
Any thoughts?
Mark M.
jdub: not really
first, adapters-in-adapters is not common
jdub
Yeah, maybe I need to look at the Android source code
Yeah I noticed that on the internet
Mark M.
possibly, though since HorizontalListView is not part of Android, I'm skeptical that it will help
if somebody pointed a gun to my head and forced me to try to implement what you're trying, I'd start with my RateList sample, and replace the RatingBar with the HorizontalListView
jdub
Oh well. I've fropped about a week of time on this. I think it may be time to throw in the towel.
Mark M.
I'd call setAdapter() on the HLV in getView() of the ListView adapter, much like how I populate the RatingBar in the sample
jdub
Yeah. I thought I had done that but...
Mark M.
and then I'd pray to the deity of my choice and hope for a miracle
:-)
jdub
hahah LOL!
Ok well if I solve this, I'll let you know how.
Mark M.
it's the sort of thing that in principle should work, but I can see lots of potential "gotchas"
7:35 PM
jdub
indeed
Mark M.
if you haven't done so already, post what you can to StackOverflow, and hope that somebody else has gone down this path already
jdub
I think the gotcha's have beat me down
Mark M.
meanwhile...
tunneling: if you come up with a question later on, let me know
jdub
Ok Thanks. I'll let you know
Mark M.
EGHDK: do you have a question?
tunneling
k
EGHDK
Yeah, I'm trying to do the sample AlertDialog on the d.android site, and it keeps crashing on me.
Mark M.
what sample are you referring to?
EGHDK
I'm doing it in an onClick method. I'll post the sample.
View paste
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.pick_color);
           .setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
               // The 'which' argument contains the index position
               // of the selected item
           }
    });
    return builder.create();
}
Mark M.
and what's your stack trace?
EGHDK
So I take that code, and I put it in my own method.
I added builder.show(); to the end of it and removed the return statement.
View paste
So the last two lines are
builder.create()
builder.show()
Mark M.
just do show()
EGHDK
Okay, running it now. I'll let you know if it works.
Mark M.
I'll swing back to you in a bit to find out
meanwhile...
EGHDK
yep
Mark M.
Yurii: do you have a question?
7:40 PM
Yurii L.
sure, I've seen you were discussing HorizontalListView, I don't want implement it all my self. what is the best available lib in your opinion?
Mark M.
I'm only aware of one HorizontalListView: https://github.com/MeetMe/Android-HorizontalLis...
jdub
Concur
Mark M.
but, then again, I've never wanted to use one, so I haven't exactly searched high and low for them
EGHDK
Nope. builder.show() line shows up as the error.
Mark M.
EGHDK: what's your stack trace?
Yurii L.
you should not call show on builder, get dialog from builder.create() and call show on that dialog
Mark M.
actually, you shouldn't do either of those things, but rather use a DialogFragment, but that's a whole 'nuther story
I was focusing on trying to get past whatever configuration issue there might be with the dialog
which is why a stack trace would be awfully handy :-)
tunneling
lol
EGHDK
View paste (14 more lines)
07-11 19:41:45.427: E/AndroidRuntime(1758): FATAL EXCEPTION: main
07-11 19:41:45.427: E/AndroidRuntime(1758): java.lang.IllegalStateException: Could not execute method of the activity
07-11 19:41:45.427: E/AndroidRuntime(1758): 	at android.view.View$1.onClick(View.java:3597)
07-11 19:41:45.427: E/AndroidRuntime(1758): 	at android.view.View.performClick(View.java:4202)
07-11 19:41:45.427: E/AndroidRuntime(1758): 	at android.view.View$PerformClick.run(View.java:17340)
07-11 19:41:45.427: E/AndroidRuntime(1758): 	at android.os.Handler.handleCallback(Handler.java:725)
07-11 19:41:45.427: E/AndroidRuntime(1758): 	at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 19:41:45.427: E/AndroidRuntime(1758): 	at android.os.Looper.loop(Looper.java:137)
07-11 19:41:45.427: E/AndroidRuntime(1758): 	at android.app.ActivityThread.main(ActivityThread.java:5039)
07-11 19:41:45.427: E/AndroidRuntime(1758): 	at java.lang.reflect.Method.invokeNative(Native Method)
07-11 19:41:45.427: E/AndroidRuntime(1758): 	at java.lang.reflect.Method.invoke(Method.java:511)
07-11 19:41:45.427: E/AndroidRuntime(1758): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-11 19:
...
Mark M.
after your cut-and-paste work, what did you wind up with as your AlertDialog.Builder constructor call?
7:45 PM
EGHDK
My code
View paste (14 more lines)
public void onClickPicture(View view) {



String[] items = {"Take Picture", "Add from Gallery"};

//List Dialog

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

    builder.setTitle("Hello")

          .setItems(items, new DialogInterface.OnClickListener() {

              public void onClick(DialogInterface dialog, int which) {
...
Mark M.
bzzzt
never use getApplicationContext() unless you know SPECIFICALLY WHY you are using getApplicationContext()
and, in general, never use getApplicationContext() for GUI-related operations
EGHDK
Oh goodness.
Mark M.
here's the best blog post on this subject I have ever seen: http://www.doubleencore.com/2013/06/context/
basically, all Contexts are not created equal
EGHDK
Is it bad that I have read that... but I still don't full comprehend context?
Mark M.
yeah, well, Context isn't one of Android's better design decisions, IMHO
EGHDK
So what's the general rules on Contexts?
Mark M.
click that link, scroll down to Context Capabilities, and look at the table
tunneling
hey, i have a question now
Mark M.
personally, my basic rule is what I stated earlier: only use getApplicationContext() when you know *why*
(tunneling: be with you shortly!)
for example, if you need to create a singleton that in turn needs a Context, the Application is the best answer, as it does not leak memory
(at least, not intrinsically)
EGHDK
so when I do getApplicationContext inside of an application for a dialog... there is no context?
Mark M.
it's more that there's no GUI
Application is not tied to the UI framework the way an Activity is
EGHDK
I'm just trying to understand the table.
Mark M.
and since an Activity is a Context, use `this` instead of `getApplicationContext()`
(and I *so* wish this chat supported Markdown syntax...)
EGHDK
Activity extends context?
Mark M.
sure
7:50 PM
EGHDK
So I purely throw in "this" for where I need context in my case right now?
Mark M.
the first 3 columns represent classes that inherit from Context
correct
(since I presume that `this` is an Activity)
let me give the others a shot, and I'll be back with you later
EGHDK
yep
Mark M.
tunneling: you indicated that you have a question
EGHDK
Oh god. "this" worked. Is there anything in logcat thats a tipoff to doublecheck context?
Mark M.
you mean besides cryptic stack traces? :-)
tunneling
yes
Mark M.
IOW, not really
tunneling: go ahead
EGHDK
"IOW"?
Mark M.
IOW = in other words
EGHDK
Thanks.
Big help.
I'm going to reread that article
tunneling
speaking of Context... I am working on an app where I have to access a sqlite db. is it best to instansiate my helper class in the an Application class?
Mark M.
um
tunneling
a bit more..
essentially i have activities getting data from the db, and a service running in teh background that will obtain async data from a server
Mark M.
if you are going to use the database across multiple threads, you want some sort of singleton
7:55 PM
tunneling
ok, so should "singletons" be brought to life in the Application class?
Mark M.
personally, I'd make the singleton be a static data member, but use Application as the Context for the SQLiteOpenHelper constructor
tunneling
I can't see how they survive otherwise
Mark M.
Application and static data members have the exact same lifespan = the life of the process
there's no particular advantage for extending Application and putting stuff there IMHO
and there's a definite cost, in that you can only have *one* Application, whereas you can have as many static data members as you need
EGHDK
"but use Application as the Context" you mean "getApplicationContext()" right?
Mark M.
yes, getApplicationContext() returns an instance of Application (or your designated subclass thereof)
tunneling
oh.. i've never tried that. so if I first call my db helper singleton from an activity, that instance will not be GC'd?
Mark M.
objects held in static data members are not garbage collected
tunneling
awesome.. didn't know that!
Mark M.
if you null out the static data member, *then* the formerly-referenced object is eligible for GC, if nothing else is holding onto it that prevents GC
tunneling
ok
Mark M.
static data members, Application subclasses, and running threads are the two main things that prevent GC of objects in Android
EGHDK
Is there a table for what gets GC'd and what doesn't? Similar to that context table you prodivided?
Mark M.
er, three main things
EGHDK: generally speaking, that's Java, not Android
EGHDK
Got it
Can you explain what you mean by "static data member"?
tunneling
i have a couple of classes that i need to be singletons. i helper for communicating with the Service and a helper for the DB.
Mark M.
hmmm... the "helper for communicating with the Service" sounds fishy
tunneling
sort of like IABhelper
8:00 PM
Mark M.
sorry, I have no idea what IABhelper is
Indigenous Aborignal Bonobos helper?
tunneling
why yes, yes it is.
jdub
Mark u r good!
tunneling
in the android samples, they have some little app for illustrating the In App Billing
Mark M.
ah! in-app billing!
tunneling
better than in-app bonobos
Mark M.
(somewhere, a bonobo slinks off in dismay)
tunneling
lol
jdub
:-)
EGHDK
lmao
tunneling
so anyway, i was using their helper as a go-by ..
launches a thread for the network operation, then calls the callback when it's done. i want it as a singleton because I don't want other calls going into the service and locking me up
Mark M.
um, OK, you lost me there
and, let me take questions from the others, and I'll try to get back to you a bit later in the chat
jdub: do you have another question?
tunneling
you asked me if i had a question ;)
jdub
Just one:
Should I start the bottle of scotch before returning to adapter in an adapter?
No I am all set
Mark M.
I cannot recommend coding under the influence
jdub
Hahah
!
EGHDK
CUI
or CWI
jdub
also goog
Mark M.
Yurii: do you have another question?
jdub
also good
8:05 PM
Yurii L.
sure, in application we use asynctasks for setting images on imageview in listView, backed by cursor adapter
when downloading file for item, set state downloading to db, which refresh list view with progress bar against downloading item but also blinks all the images
the question is when in your opinion AsyncTasks should be used
and when handlers
Mark M.
oh, I almost never use Handler directly
I'm either using AsyncTasks in retained fragments, or a message bus (e.g., Square's Otto)
Yurii L.
do you have any good examples on otto in your book?
Mark M.
not yet
probably in Version 5.1
I plan on adding an Event Buses chapter
Yurii L.
thanks, looking forward
Mark M.
covering Otto and greenrobot's EventBus, with analogies back to LocalBroadcastManager
in the specific case of "setting images on imageview in listview", I'd use Picasso
as it handles all the threading, recycling, etc.
tunneling
have you tried volley?
Mark M.
I cover Picasso in the context of using it for HTTP operations, but it works just as well for local files
Yurii L.
I wanted to use it but I probably will need encryption and Picasso doesn't support one
Mark M.
tunneling: no -- you can read me thoughts on Volley in the Internet chapter in the current book edition
8:10 PM
Yurii L.
I think that is what I will do. thanks
Mark M.
Yurii: they might have a hook where you could plug in decryption, though I do not recall one
EGHDK: do you have another question?
EGHDK
Yeah two quick questions.
Can you explain what you mean by "static data member"?
When you were talking about GC.
jdub
I have to bounce. Take care folks Jdub!
jdub
has left the room
Mark M.
jdub: see you later!
EGHDK: you will also hear them referred to as static variables or class variables
standard Java concept
EGHDK
Okay cool thats what I thought, I was just making sure.
Mark M.
EGHDK
I'm not great at java yet... sadly
Mark M.
it'll grow on you
EGHDK
Secon question is, I tried to do a Toast in my dialog and I tried "this" and it wouldn't compile.
When I used "getApplicationContext" in toast... it worked.
Mark M.
you may be using an anonymous inner class (e.g., new MyInterfaceName() {})
in that case, this refers to the instance of the inner class
use MyActivityClassName.this to get the "this" that represents the activity
EGHDK
Okay, so MyACtivityClassName.this works... but what do you mean "anonymous innner class"?
Mark M.
EGHDK
Okey Dokey
8:15 PM
Mark M.
again, another standard Java concept
though one that IMHO gets used a bit more in Android than elsewhere
in fact, I've been steadily removing them from my samples where practical, just to reduce confusion
tunneling: do you have another question?
tunneling
You and I talked about this connection service of mine extensively in the past. I still haven't conquered it. So my Service creates a Thread that creates a socket with a Remote Service. The job of the Service is to maintain the connection, write and receive messages. I have a library that encodes and decodes the messages from the bits and bytes. So the Connection can receive messages from the Remote Service and the User can send messages by clicking on a UI. I can't seem to nail it down, but somehow the Service intermittently locks up. Thus forcing Users to send me nasty-grams and having to FC their apps.
So, back to my original question.. im trying to use the Helper to the Service to marshal messages.
Mark M.
I can see a helper, just not the singleton part
tunneling
what if I have two fragments both with UI elements that can send commands?
Mark M.
I fail to see the problem
tunneling
what if the User clicks an element in each fragment at the same time?
Mark M.
have a queue
commands go in the queue
tunneling
i don't know how to do that!
lol
Mark M.
the service pulls commands out of the queue to process them
use a LinkedBlockingQueue
tunneling
what page?
Mark M.
and a thread or thread pool
LinkedBlockingQueue is standard java, in java.util.concurrent
I may use it in one example, hold on
tunneling
ok.. take a runnable?
Mark M.
I cover it in the Remote Services chapter
tunneling
ok, ill check it out.
8:20 PM
Mark M.
basically, when commands come in (in this case, via a binder), I pass them to a ThreadPoolExecutor that is backed by a LinkedBlockingQueue
8:20 PM
Mark M.
thread(s) from the pool will process the commands as they come in
in my case, my pool has only one thread
tunneling
that's exactly what I need
I'm not creatign multiple connection threads
Mark M.
give ThreadPoolExecutor and a queue a whirl
tunneling
so if i can... i'm also curious if there is a technique for determining why a Thread doesn't go away.
Mark M.
common Java pattern
it doesn't go away because you haven't fallen out of your run() method
the thread could be blocking on I/O or something
tunneling
true, but doesn't it remain in the run() method if there are lingering references
Mark M.
references have nothing to do with it
once you exit run(), the thread terminates
the *object* remains
but the OS thread is effectively defunct
tunneling
so in eclipse, when you looking at the the Thread list.. would a terminated thread remain in that list?
Mark M.
ummm... I don't think so, but I haven't experimented with that precise scenario
I don't run into thread problems all that often, and so I rarely find myself poking through the threads list in DDMS
there's a status column IIRC
8:25 PM
Mark M.
if anyone else has any questions, chime in, as we're running out of chat time
EGHDK
Since I'm having trouble with basic java concepts, is there any "one" resource I should look into? Oracle docs... or something else? Like if I was to spend this entire weekend to read one source of Java, what would I read?
tunneling
occasionally i need to terminate the connection thread, and connect to a different server. i'm having trouble making the original thread go away.
EGHDK
I never really did Java development before Android.
Yurii L.
what http library would you recommend to use?
Mark M.
EGHDK: I learned Java back when dinosaurs roamed the Earth, so I'm not up on the latest educational resources
EGHDK
bah.
Stupid dinos
Mark M.
tunneling: not sure what to tell you
Yurii: that depends on what sort of HTTP I/O you're trying to do
Retrofit is pretty nice for JSON
Picasso is pretty nice for images
Yurii L.
JSON
tunneling
i'll just go away and sit in the corner
Mark M.
tunneling: it's more that I have no good way of telling you what your problem is
tunneling
yea, it's a bit vague. i think my wife could tell you my problem...lol!
she has a list.
Mark M.
Yurii: Retrofit works nicely with JSON Web services
EGHDK
lmao
^
Mark M.
I also just heard about https://github.com/koush/ion today, and it looks rather interesting
tunneling
and they aren't in a Queue either.
Yurii L.
but it is not a REST web service :(
tunneling
koush also has a nice Async library.. ion uses it
Mark M.
Yurii: so long as the requests are basic GET operations, Retrofit should work
tunneling
it helps to look at the tests to get a good feel for it's capabilities.
8:30 PM
Yurii L.
thanks, I will look deeper into retrofit and ion
Mark M.
anyway, that's a wrap for today's chat
the transcript will be posted to http://commonsware.com/office-hours/ shortly
tunneling
that's a wrap!
Mark M.
next chat is Tuesday at 4pm Eastern
EGHDK
Next Tuesday
Mark M.
have a pleasant day!
EGHDK
I'm going to go make myself a wrap.
tunneling
bigh.. thanks Mark
tunneling
has left the room
EGHDK
has left the room
Yurii L.
has left the room
Mark M.
turned off guest access

Tuesday, July 9

 

Office Hours

People in this transcript

  • EGHDK
  • jdub
  • Mark Murphy
  • tunneling
  • Yurii Laguta