Office Hours — Today, November 5

Friday, November 1

Nov 5
3:50 PM
Mark M.
has entered the room
Mark M.
turned on guest access
3:55 PM
Guy
has entered the room
Mark M.
hello, Guy
how can I help you today?
Guy
Hi
a listview with an imageview and textviews. what is the best way to do it with a simplecursoradapter and viewbind or arrayadapter and viewholder
Mark M.
if your data is in the form of a Cursor, use a CursorAdapter
4:00 PM
Mark M.
if your data is in the form of an ArrayList, use an ArrayAdapter
Guy
its from sqlite db so I guess thats a CursorAdapter?
Mark M.
I would certainly start there
Bob R.
has entered the room
Bob R.
Hi Mark
Mark M.
it is possible that you will have other reasons why you need to convert the data from a Cursor to an ArrayList
but, until you have a need for that, stick with the Cursor and CursorAdapter
Guy
OK thanks.
Mark M.
let me take a question from Bob, and I will be back with you shortly
Bob: hi -- do you have a question?
Bob R.
Hi Mark
Quick question
I noticed that variables are initialized to null
Is this a statdard?
Mark M.
I would describe it as more of a personal style
you do not see that style used much nowadays
I am a bit "old school" in my Java :-)
Bob R.
I thought that this may cause the NullPointerException type of errors I've been getting
Mark M.
no, that is because you never set it to something other than null
the data members always start null, even if you do not explicitly assign it
Bob R.
I thought that it would be best to avoid null where possible. But I'm still a newbe to java
4:05 PM
Mark M.
(exception being primitive data members, which have a type-specific default initialization value, like 0 for an int)
null means "we do not have an appropriate object back"
er, "we do not have an appropriate object *yet*"
VineelAryan8125
has entered the room
Bob R.
OK. I have my app where the internal file is saved but later the file cant be opened
I thought that I may have null chars in the file
VineelAryan8125
has left the room
VineelAryan8125
has entered the room
Mark M.
that is unlikely to be the source of a NullPointerException
VineelAryan8125
Hello Mark
Mark M.
moreover, if you read your stack trace, you will know what line is causing the NullPointerException
Bob R.
OK, any suggestions as to where I cal look
Mark M.
I would recommend reading the chapter on debugging in your Java book
(Vineel: hello! I will be with you shortly!)
Bob R.
How about on an andeoid phone?
Mark M.
I do not understand that question, sorry
Bob R.
OK I;kk read it again
Mark M.
Bob: let me take questions from the others, and I will be back with you shortly
VineelAryan8125: do you have a question?
Bob R.
Checking what the error is.
VineelAryan8125
yeah
Bob R.
OK thanks for your time.
VineelAryan8125
is there a way to implement the navigation drawer in multipane?
Mark M.
what is "multipane"?
VineelAryan8125
in tablets
4:10 PM
Mark M.
you can certainly have a navigation drawer as part of a master-detail or similar "multipane" activity
VineelAryan8125
i've searched for it but ended up with some bad examples. Do you know of any good examples out there?
Mark M.
it would be no different than any other use of a navigation drawer, so I fail to understand what your concern is
the navigation drawer does not care how many inches of screen size you have, or what classes are organizing the widgets
what specifically are you worried about?
VineelAryan8125
im
Bob R.
has left the room
Mark M.
VineelAryan8125: let me take a question from Guy, and we can follow up on this shortly
Guy: do you have another question?
VineelAryan8125
i mean, on the tablets, the navigation drawer shouldn't be there, but with the master-detail fragment
ok
4:15 PM
Guy
CursorAdapter do you have some example code?
Mark M.
the book covers CursorAdapter
I use it in several places
Guy
thank you
Mark M.
VineelAryan8125: OK, back to you
the "master" in "master-detail" represents some collection of content
the navigation drawer could let you get to other such collections of content
or to other major landing points within the application (e.g., the shopping cart in an e-commerce app)
VineelAryan8125
on the phones, the navigation drawer is usually the master right?
Mark M.
no
the navigation drawer is never the master
VineelAryan8125
ok
Mark M.
in the Gmail app, for example, the "master" would be a list of conversations, and the "detail" would be the messages in a conversation
the navigation drawer lets you get to different accounts, different labels, etc.
4:20 PM
VineelAryan8125
oh i get it
Mark M.
let me go back to Guy for a bit, and I can take another question from you shortly
Guy: do you have another question?
VineelAryan8125
sure
Guy
thanks. :-) I could recycle the CursorAdapter with a spinner.
Mark M.
sorry, but I do not understand the "recycle" part of that comment
you can put a CursorAdapter in a Spinner, though
Guy
yes
OK thanks :-)
4:25 PM
Mark M.
VineelAryan8125: do you have another question?
VineelAryan8125
i'm trying to load images using the dropbox sdk. The images are loading fine, but sometimes, the asynctask throws a thread pool exceeded exception. So, i've copied the asynctask from the android source and modified the thread pool to 9999. Will that do good or will it create any performance issues or any other problems?
Mark M.
that is not a great solution, as too many threads can cause too much overhead from task switching
Richard L.
has entered the room
Mark M.
I would leave the thread pool where it is (or have it smaller, even), but use a larger queue
or, better yet, use a different algorithm for choosing what to load, as downloading 9999 images will be much too much bandwidth
(Richard: I will be with you shortly!)
Richard L.
Thank you sir.
VineelAryan8125
ok thanks
Mark M.
for example, only download images when the user scrolls, using placeholders in the meantime
VineelAryan8125
i'm already using a viewholder in my listview
Mark M.
that only deals with the ImageView, not the source of images
VineelAryan8125
oh sorry, place holders, ok
Mark M.
Richard: do you have a question?
Richard L.
Yes, I am still formulating it.
1 minute.
Mark M.
wow -- complicated question! :-)
Richard L.
LOL
4:30 PM
Mark M.
while Richard is formulating, if anyone else has a question, chime in
Richard L.
View paste
Mark, glad to be here and very thankful you do this.

I have an application that essentially tells you your current data transfer rate.  So it shows you your current upload and download speeds.  My app runs in an ongoing notification in the notification drawer, and it can also show you the info as a widget.

My question: What is the best way to do this with regards to services?  Should I have a base service that always runs, allowing the notification drawer service and the widget service to get the info from the base service.  This totals three services.

Or, should I make the notification service and widget service completely separate.  Both gathering the necessary data info on their own?

All help is greatly appreciated.
Mark M.
how frequently are you trying to update this data?
Richard L.
every second. I currently use a handler for my notification and an alarm service for my widget.
Mark M.
why are you treating your Notification-update logic differently than your app widget-update logic?
4:35 PM
Richard L.
I use an alarm service for my widget because I have read that when updating widgets very frequently, your best bet is to use an alarm service. This way, it will not keep the device awake when it is asleep.
Mark M.
that is only when contrasted with android:updatePeriodMillis, which, under the covers, uses a _WAKEUP alarm
Richard L.
yes, but android:updatePeriodMillis will run a max of once every 30 minutes, correct?
Mark M.
correct
my point is that what you read is immaterial for your use case
Richard L.
I am sorry, what do you mean by that?
Mark M.
never mind
first, let me state for the record that I hate apps like this with the fiery passion of a thousand suns
as it's a constant battery drain, doing IPC every second
that being said
you have two choices: evil and another form of evil
Richard L.
Mark, my app does not affect my battery life at all. Not even 1% shows up in the battery use. But I hear you.
Mark M.
one option is to go with zero services, having AlarmManager invoking a BroadcastReceiver every second and doing the calculations and app widget + Notification updates
4:40 PM
Richard L.
WOAH. That is snazy.
Mark M.
the other option is to go with one everlasting service, using something (I'd go ScheduledExecutorService instead of Handler, personally to trigger the every-second calculations and app widget + Notification updates
both are anti-patterns, in that AlarmManager is not designed for that frequent of work, and that services are not designed to be everlasting
in terms of CPU load, the everlasting service would be the better option
RAM impact is roughly equivalent
(i.e., both should consume about the same heap space, and hence system RAM)
and the everlasting service probably makes coding a bit esier
er, easier
so, if somebody pointed a gun at my head to try to force me to write this app, I'd lean in that direction
regardless, *please* let the user configure the polling period
Richard L.
LOL
Mark, I run my app all the time on my device, and I am telling you the battery life hit is barley anything.
My calculations that run are very minimal.
Mark M.
the effect will be worse on older/slower devices, I imagine
moreover, the "battery blame screen" would not necessarily blame your app
the calculations are dwarfed by the IPC calls, I would guess
Richard L.
Good point.
4:45 PM
Mark M.
anyway, that's my recommendation
Guy: do you have another question?
Richard L.
Because I am currently using a handler for my notification updates, does that still run when the phone is asleeep?
oh sorry, I will wait.
Mark M.
Vineel: do you have another question?
VineelAryan8125
yes
In some apps there is a toggle button to toggle between list and grid type. What is the technique behind that? Are they really toggling between listview and gridview or is it gridview changing the numColumns programmatically?
or neither?
Mark M.
off the top of my head, I would assume that they are toggling between ListView and GridView, but I have not investigated it
you can use uiautomatorviewer to find out, for a specific app, what they are doing
if you only see a GridView, then they are changing the # of columns
if you see a GridView and a ListView, with one having visibility of GONE, they are toggling between the two, most likely
VineelAryan8125
can i use uiauomatorviewer on phone?
Mark M.
yes
very very handy for deciphering how a production app does its UI
VineelAryan8125
ok
thanks mark
Mark M.
Richard: back to you
Handler does not keep the device awake
VineelAryan8125
that's it for now. thanks for your answers. See you later
4:50 PM
VineelAryan8125
has left the room
Mark M.
neither will ScheduledExecutorService
Richard L.
Thank you Mark.
Mark M.
and neither will AlarmManager, if you are not using a _WAKEUP alarm
if anyone has any questions, chime ni
er, chime in
Richard L.
Since I am using one service, as per your recommendation, it is possible to get my widget to update without having a specific service dedicated for updating my widget?
Mark M.
yes
just use AppWidgetManager and updateAppWidget()
just as you use NotificationManager and notify()
Richard L.
And updateAppWidget() can be called more than once every 30 mins?
Guy
Thanks Mark. I was reading about the CursorAdapter
Mark M.
Richard: the only tricky part is knowing your app widget ID(s), which you can cache (in the service or as a static data member) and keep persistently in case you get kicked out of RAM
Richard L.
So I would have a total of two services then, assuming a user has added a widget?
Mark M.
no, just one
your same service would update the Notification and the app widget based upon your every-second poll
4:55 PM
Richard L.
Got it, sorry, I misread.
I am all set Mark. Thank you very much for offering this service, pun intended. Have a good day.
Mark M.
happy to be useful!
Guy
Have a nice day :-)
Guy
has left the room
5:00 PM
Mark M.
that's a wrap for today's chat
the transcript will be posted to http://commonsware.com/office-hours/ shortly
the next chat is Thursday at 10am Eastern
have a pleasant day!
Richard L.
sweet, I was copy and pasting!
Richard L.
has left the room
Mark M.
turned off guest access

Friday, November 1

 

Office Hours

People in this transcript

  • Bob Richardson
  • Guy
  • Mark Murphy
  • Richard Lucas
  • VineelAryan8125