Office Hours — Today, December 13

Yesterday, December 12

Dec 13
9:55 AM
Mark M.
has entered the room
Mark M.
turned on guest access
10:00 AM
JY
has entered the room
Mark M.
howdy, JY
how can I help you today?
Gabriele
has entered the room
Mark M.
howdy, Gabriele!
JY
Hi Mark, I have some trouble regarding SQLiteDatabase. I can't solve it even after I looked for help on stackoverflow
Gabriele
Hi :) I don't have questions for now, just here observing! :P
Mark M.
Gabriele: OK
"it didn't work" is a useless description of your symptoms
please explain *precisely* what "didn't work"
oh, wait, never mind
JY
OK, the app crashes.
Mark M.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demostudio.restaurants/com.demostudio.restaurants.AllRestaurants}: java.lang.IllegalArgumentException: column '_id' does not exist
you are not creating an _id column
your CREATE TABLE has RestaurantID, not _id
10:05 AM
Mark M.
and CursorAdapter needs the Cursor to have _id as a column
your query is trying to rename colRestaurantID as _id
but you do not have a colRestaurantID column
you have a RestaurantID column
you appear to be mixing up your Java constants (colRestaurantID) with your chosen representation in your SQL (RestaurantID)
JY
Oh ok, so in my SQL, I need to use RestaurantID?
Mark M.
in the SELECT statement for rawQuery(), yes
I am a bit surprised that you got the error you did -- I would have expected SQLite to complain about missing columns
your other column names in that SELECT statement also need to be adjusted
JY
How about in other cases like onCreate where I also use SQL?
Or is it just in rawQuery?
Mark M.
you need to be consistent
in onCreate(), you are using the *value* of the *Java constant*, whereas in rawQuery(), you are treating "colRestaurantID" as a string itself
so, for example, you could change your rawQuery() to concatenate things the way your CREATE TABLE statement is constructed
10:10 AM
JY
OK, I think I kind of understand. Alright, let me try and I will get back to you. Thanks Mark.
Mark M.
OK
if either of you has a question, while nobody else is in the chat, feel free to chime in
Gabriele
eheh thank you :)
I have one, if I have to execute http request, it's good to execute them on a separate thread (maybe using asynctask), but If I need to wait its result (so it should be synchronous) what can I do?
Mark M.
rework your UI so that you do not "need to wait its result"
10:15 AM
Gabriele
what I'm doing it's a client which sends the request to BlueSocket (maybe you know)
it's a public wifi of the university
but you need to login before access
I'm sending login data, but I have to wait its answer before proceed
Mark M.
no, you need to continue doing your work after the answer is ready
that does not mean that it is synchronous
Gabriele
ah
Mark M.
continue your work from `onPostExecute()`
disable anything in the UI that cannot be used while you are waiting for the result
Gabriele
another question, is it a bad thing to execute a new asynctask from onPostExecute?
Mark M.
no, that works OK
you cannot start an AsyncTask from doInBackground()
but starting one from onPostExecute() is within reason
however, you might consider simply merging the two tasks
Gabriele
ok, so I can move the other task inside onPostExecute
ah
then I will merge them
Mark M.
JY: I added an answer on your SO question that may help
JY
OK, it works now!
I can't believe this simple thing has been bothering me in the past few days..
10:20 AM
Mark M.
one suggestion: it is standard in Java to have your static final data members be in all caps, such as COL_RESTAURANT_ID
Gabriele
another question, it seems that something in my wikipedia app is incompatible with android 4 (I'm saying it because I can't select it from my emulator dropdown selector), but I remember that time ago I had warning saying something about incompatibility on methods with a specific version
Mark M.
that might help you keep track of the difference between the literal SQL and the Java representations of those SQL pieces
Gabriele: if you cannot choose an emulator, that typically is due to your <uses-sdk> element
mostly, you will see that for older emulators, where android:minSdkVersion is set higher than the emulator
Gabriele
View paste
android:minSdkVersion="11"
            android:targetSdkVersion="17"
Mark M.
you should be able to run that on any emulator for API Level 11 or higher
JY
Another related question. Can I not concatenate things but use RestaurantID and the like in onCreate to create table? I think it looks cleaner that way.
OK, I will do that then. Thanks.
Mark M.
JY: you are welcome to concatenate or not, you just have to be consistent about the values that you are concatenating
Gabriele
ah, I had to restart the ide after adding it
JY
OK, I saw your reply on SO. Thanks!
10:25 AM
Gabriele
View paste
I'm getting an exception only on android 4.0.* (I've tested it on 4.1 and 4.2 and it seems working): FATAL EXCEPTION: main
        java.lang.NoSuchMethodError: android.view.ActionMode.setTitleOptionalHint
Mark M.
that method was added in API Level 16
you will need to avoid calling it on older versions of Android
Gabriele
but I'm only doing host.startActionMode, is this wrong?
Mark M.
somebody is calling setTitleOptionalHint()
Gabriele
oh no true
Mark M.
you can use Build.VERSION to conditionally call that method on supported versions
Gabriele
but if I'm using minsdkversion to 11, shouldn't the ide tell me "ehy you can't use this method"?
Mark M.
in theory, Lint should be yelling at you
do you have a @TargetApi annotation on the method where you are calling setTitleOptionalHint(), or on the class?
or perhaps a @SuppressLint annotation on the method or class?
10:30 AM
Gabriele
no I don't think I've ever added it and I can't see this in the file
Mark M.
try manually running Lint (right-click over project, choose Android Tools > Run Lint from the context menu)
JY
So now I have a listview XML layout and also a textview XML layout. What I actually want is only the textview. However, if I change setContentView(R.layout.activity_listview) to setContentView(R.layout.activity_all_restaurants) which is the textview XML, it won't work. Is there anyway I can fix this?
Mark M.
JY: that does not make much sense
why are you querying a database for all restaurants if you do not want to show a list of those restaurants?
your "textview XML layout" is for your rows in your list, as you have things presently set up
(at least, based on the code in the SO question)
JY
I want a list of restaurants, but right now what it is giving me is many lines of the same restaurant.
It gives me many lines of Restaurant AAA and then many lines of Restaurant BBB.
Mark M.
then that is what is in your database
10:35 AM
Mark M.
your code is populating the ListView rows based on the query results
if you are seeing "many lines of Restaurant AAA and then many lines of Restaurant BBB", then that is what is coming back in the query results
JY
OK. I might have inadvertently created many records when I ran the app.
Mark M.
you might consider clearing your data or uninstalling the app, to start with a fresh database, then go from there
Gabriele
hm no Lint isn't saying anything
Mark M.
Gabriele: perhaps you accidentally disabled that Lint check
that could be at the project level or the workspace level
10:40 AM
Mark M.
for the project, Project Properties > Android Lint Preferences
Gabriele
how can this option be called?
Mark M.
how can what option be called?
Gabriele
this one which enables warnings
Mark M.
by default, it is enabled
Gabriele
if I'm using a method I can't use
Mark M.
however, you might have accidentally told Eclipse to ignore it.
View paste
as I was saying, 
that could be at the project level or the workspace level
for the project, Project Properties > Android Lint Preferences
type ~ignore in the search field
and see what comes up
or, search for NewApi, which I think is the actual Lint check in question
yeah, NewApi appears to be it
check what the severity is on it -- if it is set to "ignore", switch it to "error"
JY
I thought each XML file correspond to one layout. How can two XML files be used in a source file and display both of them? In my case which is listview XML file and textview XML file. How does it work actually?
Gabriele
maybe is something to do with intellij then, I can't find it
Mark M.
Gabriele: oh, sorry, I though you were using Eclipse
I have no idea what/how IDEA does in this area, sorry
10:45 AM
Gabriele
I hate eclipse because it always gives a lot of error and I have to spend my time resolving eclipse problems TT
Mark M.
JY: when you search your AllRestaurants class for t
er, let me try that again
JY: when you search your AllRestaurants class for activity_all_restaurants, what do you see?
you should see that you are referencing that layout resource in your SimpleCursorAdapter constructor
this indicates a layout resource to use for rows in your ListView
this is separate from the layout you use for your overall activity
JY
OK.
Thanks!
Mark M.
I would recommend that you review my book, pages 1-200, and then 467-484 (using page numbers from the current Version 4.4)
specifically, pages 171-200 cover the use of AdapterViews like List View, and 487-484 cover using a database
JY
I did read those pages quite a few times actually. I guess I have to spend more time on them then.
10:50 AM
Mark M.
in the chapter on AdapterView, you will see how we use layout files to define rows in a ListView, using an ArrayAdapter
SimpleCursorAdapter and a database work on the same principle
JY
I closed my emulator and reopen it and then relaunch the app, but I am still seeing the same thing (many lines of the same restaurant). Does closing the emulator not uninstall the app?
Mark M.
no, it does not, any more than rebooting your phone uninstalls the apps on your phone
to uninstall an app on the emulator, you can go into Settings, the same as you would on your phone
10:55 AM
JY
Mark, any way to save this conversation other than copy and paste?
Mark M.
it will be archived to http://commonsware.com/office-hours/ shortly after the chat is over
JY
Ok, great.
Gabriele
nice
JY
Alright, thanks Mark.
JY
has left the room
Gabriele
thank you very much Mark, see you the next time!
Mark M.
next chat is Tuesday, 10am Eastern
JY
has entered the room
11:00 AM
Gabriele
ok, thank you
JY
I uninstall the app and try to relaunch it, then it crashes again this time.
Mark M.
JY: we will have to pick that up in the next chat
JY
I didn't change anything in the codes.
Mark M.
as this chat hour is over
JY
OK. Thanks
Mark M.
the next chat is Tuesday at 10am Eastern
have a pleasant day, all!
JY
You too.
JY
has left the room
Gabriele
has left the room
Mark M.
turned off guest access

Yesterday, December 12

 

Office Hours

People in this transcript

  • Gabriele
  • JY
  • Mark Murphy