Office Hours — Today, March 21

Tuesday, March 19

Mar 21
7:20 PM
Mark M.
has entered the room
Mark M.
turned on guest access
7:25 PM
Kevin M.
has entered the room
Mark M.
howdy Kevin!
Kevin M.
Hi there
Mark M.
how can I help you today?
Kevin M.
I'm having problems with Fragments, ActionBarSherlock and menus
Gabriele
has entered the room
Kevin M.
I can't get menus to show for the 2nd fragment
Gabriele
hello :)
Mark M.
(howdy, Gabriele -- I will be with you shortly!)
Kevin: do you mean that both fragments are on the screen at the same time?
7:30 PM
Kevin M.
No, I do a replace transaction. It's a pretty simple app but the menu seems to disappear.
I can click on the menu and it seems to do something but doesn't show
Mark M.
"but doesn't show"... show what?
Kevin M.
Doesn't show the text
Mark M.
"show the text"... what text?
Kevin M.
Menu text. No icon
Just 1 menu item
Mark M.
what is your android:minSdkVersion?
Kevin M.
I've tried about every combination. Handling everything in the activity etc
View paste
	<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>
Mark M.
and what OS version are you testing this on?
Kevin M.
Mostly 4.1
Mark M.
are you pressing the device/emulator MENU button, or the ... affordance in the action bar?
Kevin M.
I'm doing this mostly on real phones. I have done it in the emulator but get the same results
Mark M.
when you "click on the menu", are you pressing the device/emulator MENU button, or the ... affordance in the action bar?
Kevin M.
I don't have a submenu. Just :
View paste
<menu xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:title="@string/edit" android:showAsAction="always" android:id="@+id/edit" />
</menu>
7:35 PM
Kevin M.
so I don't see ... On the first fragment I see "New"
Mark M.
I will ask this one more time
View paste

when you "click on the menu", are you pressing the device/emulator MENU button, or the ... affordance in the action bar?
Kevin M.
I'm not pressing anything. The menu just shows up on the first fragment. I can press that menu but other fragments don't show a menu item
Mark M.
earlier in this chat, you wrote: "I can click on the menu and it seems to do something but doesn't show"
I have no idea what this means to you
Kevin M.
This is for the ActionBar
So on the far right I have a menu item that is just text that says "New". On the next screen/fragment, I want to use another menu, say "Edit"
Mark M.
when you say "menu", are you referring to the <item> from your bit of XML above?
Kevin M.
Yes
Mark M.
that is not a "menu"
it is an action bar item
Kevin M.
Ok, but you use menu code
7:40 PM
Kevin M.
Can I send a screenshot?
Mark M.
feel free to upload one
in the meantime, let me take a question from Gabriele
Gabriele: do you have a question?
Gabriele
I'd like to provide a small guide for my app to users, is there some guidelines/layout/library/example to do it or from I can get the idea?
Kevin M.
Mark M.
Gabriele: by "small guide for my app", what do you mean? online help? something else"?
Gabriele
yes, offline help, or something like this
Mark M.
I think using a WebView is fairly conventional
put your HTML/CSS/images in assets/
use file:///android_asset/... as the URL with the WebView
Gabriele
ok, I've understand, thank you
Mark M.
richly formatted, can link to additional online resources, etc.
OK, back to Kevin
Kevin: is your screenshot showing your activity with the first fragment, or with the second fragment?
Kevin M.
Does the "New" above make sense?
First fragment
Mark M.
OK
Kevin M.
When I click on a name, I replace that fragment with a new one
Mark M.
OK
Kevin M.
that's when the menu disappears
Mark M.
well, the New entry should go away, if you defined that in the first fragment's onCreateOptionsMenu()
the action bar will then have anything you declared in the activity's onCreateOptionsMenu() and the second fragment's onCreateOptionsMenu()
7:45 PM
Kevin M.
Right, but the other fragment has their own menu who's code gets called but not shown
Mark M.
did you call setHasOptionsMenu(true) on the second fragment?
Kevin M.
I can debug it and see that the code is being called
Yes
Gabriele
thank you Mark, see you the next time, have a good day, Kevin too. :)
Mark M.
can you paste in your onCreateOptionsMenu() method from the second fragment?
Kevin M.
You too
Mark M.
Gabriele: see you later!
Kevin M.
View paste
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		if (menu.findItem(R.id.edit) == null) {
			menu.clear();
			inflater.inflate(R.menu.edit_menu, menu);
			//			menu.findItem(R.id.newItem).setVisible(true);
		}
	}
Mark M.
why are you calling clear()?
Kevin M.
I was getting that method called several times
Mark M.
then you have other problems, perhaps
you should only need the inflate() call
the setVisible() should crash, as you do not have R.id.newItem, assuming that the XML you pasted earlier is from R.menu.edit_menu
Kevin M.
Yeah, that's commented out. I was trying to have 1 menu with 3 items that I would try and enable just 1
Mark M.
oh, sorry, didn't realize that was a word-wrap issue -- I thought the comment was on a blank line
7:50 PM
Kevin M.
It's possible the problem was that when I called setVisible(true) that caused a problem as initially all items where not visible
Mark M.
if I recall correctly, the EmPubLite sample shows action bar items coming and going based on fragments being added and removed from the screen, in Tutorial 19
I also have a recent sample showing action bar items coming and going related to pages in a ViewPager, though that is probably less relevant here
Kevin M.
ok. I'll take a look at it
Mark M.
your "I was getting that method called several times" suggests that you have some other issues, such as perhaps having the fragment added more than once
Kevin M.
I stared with a ViewPager and that didn't work too well. Got very complicated
I was trying to call transaction.add() before I changed to replace
Mark M.
add() would not be suitable for your situation -- that would be if you wanted both the list and the second fragment on the screen at once
beyond that, there is little that I can suggest to you, other than to create a sample project that reproduces problem, and come back sometime with that project that I can take a look at
Prasanna P.
has entered the room
Mark M.
or, post that project as part of a question on StackOverflow
Kevin M.
Ok. Thanks
Mark M.
howdy, Prasanna!
Prasanna P.
Hi Mark!
Mark M.
Prasanna: the others have had a shot already, so do you have a question
?
Prasanna P.
Yes
View paste
I want to have a boolean value associated with all of my contacts in the address book.  This boolean value will indicate whether a contact is part of my team or not.  If there is no boolean value associated with the contact, the value would default to 'false'.  I also want to show all my contacts in a list view with a check mark associated with the boolean value (defaulting to 'false' if there is no value).

Question: What would be a good approach to store the boolean value associated with each contact in a manner that I can create a cursor that has all the contacts and the boolean value associated with each contact.
Mark M.
by "contact", you mean the contacts from the device's Contacts app, as seen in ContactsContract?
Prasanna P.
yes
Mark M.
in which case, I have absolutely no idea
ContactsContract makes me break out in hives
Prasanna P.
:)
7:55 PM
Prasanna P.
So is there another approach I could take?
Mark M.
I did what I could with my chapter on it, but that's as far as I have ever taken it
well, one of the identifier columns in ContactsContract is supposed to be durable
you could have your own side table in your own app, keyed by that durable identifier, where you hold your additional data
you'd have to do cleanup periodically to deal with added/removed contacts
I am under the impression that apps are supposed to be able to store other data in ContactsContract, but I do not know the details
Prasanna P.
so you mean to have the additional identifier in the durable column?
Mark M.
I am not sure what "the additional identifier" means
Prasanna P.
meant the boolean value I want to attached to a contact (in my team or not)
Mark M.
basically, your database table would use the durable identifier as a foreign key of sorts to the ContactsContract provider
Prasanna P.
ok
Mark M.
booleans make for lousy identifiers, as you can only have two of them... :-)
Prasanna P.
so when I do a cursor, how can do a join between the ContactsContract Data table and my table
Mark M.
by hand
or possibly a CursorJoiner
Prasanna P.
so use sql syntax?
8:00 PM
Mark M.
well, for querying your own table, yes
Prasanna P.
so can not use the Contacts content provider?
Mark M.
there is the Contacts content provider (bad) and ContactsContract content provider (good)
as I wrote earlier, I think there is a way you can store your data in ContactsContract directly, but I do not know the details
Gabriele
has left the room
Prasanna P.
ok.
Mark M.
if you wish to instead maintain your own side table, you'd query your table, query the ContactsContract provider, and stitch together the results
you cannot directly express a JOIN with a content provider in general, let alone between a content provider and another data source, like your own table
Prasanna P.
So how do I know the tables names for the ContactsContract?
and the columns?
Mark M.
you do not know the table names
you do not need the table names
when you query a content provider, you use a Uri
which roughly equates to a database + a table within that database
Prasanna P.
ok
Mark M.
column names are listed as constants on the ContactsContract set of classes
you'll see some examples of querying ContactsContract in the chapter on contacts in the book
Prasanna P.
ok
Mark M.
let me take another question from Kevin, and I'll be back with you in a bit
Kevin: do you have another question?
Prasanna P.
Thanks Mark! Heading out.
Prasanna P.
has left the room
8:05 PM
Prasanna P.
has entered the room
Mark M.
hello again, Prasanna!
Kevin: do you have another question?
Prasanna P.
Hi Mark, what was the column you mentioned again? :)
Mark M.
I am not sure what you are referring to
Prasanna P.
the foreign key into the ContactsContract table?
Mark M.
one of the various columns in the ContactsContract area is supposed to be a durable identifier
Prasanna P.
oh yes. I read that in the documentation
Mark M.
I think it may be LOOKUP_KEY
but I am not completely certain
did I mention that ContactsContract makes me break out in hives?
Prasanna P.
That sounds correct. I will look it up
:)
I have another question, but will wait for Kevin
Mark M.
Kevin: do you have another question?
8:10 PM
Kevin M.
has left the room
Mark M.
Prasanna: go ahead
Prasanna P.
So do you have another approach of solving what I want to do, given that ContactsContract is not very friendly?
Mark M.
well, if you want to use device contacts, you have no real choice but to deal with ContactsContract on some level
Prasanna P.
Basically I want to have a variable of my own associated with each contact on the phone that I can use to display in a list view with all the contacts
Mark M.
understood
which means that you need ContactsContract, to some degree
either you are modifying ContactsContract's data to hold your data
or you are holding your data separately and using some common identifier, foreign key-ish, to link them up
Prasanna P.
so in the Data table I think there is a column that indicated the data type and you can have your own data type. So I can create my own data type to store my data in the Data table
Mark M.
in theory, yes
8:15 PM
Prasanna P.
so for some contacts I will do this (let's say)
But in the list view I want to show all the contacts. So how would I use the content provider to get all the contacts with 'my' variable for the contacts for which it is set
Mark M.
you would include your column in your projection that you pass to your Loader, or query() on ContentResolver
I presume that the engine for add-you-own-data has some notion of default values
but, again, I have never messed with this area of ContactsContract
Prasanna P.
ok
Mark M.
as cortisone cream to combat the hives gets expensive
Prasanna P.
:)
Ok Mark. I will experiment on this. Should be a good exercise :)
Have a Good night.
Mark M.
you too!
Prasanna P.
Talk to you later!
Prasanna P.
has left the room
8:30 PM
Mark M.
turned off guest access

Tuesday, March 19

 

Office Hours

People in this transcript

  • Gabriele
  • Kevin Moore
  • Mark Murphy
  • Prasanna Perera

Files in this transcript