In this lab you will create a new activity that displays all contacts from

advertisement
In this lab you will create a new activity that displays all contacts from a contacts database and allows
a user to pick a contact.
PART 1
I.
Create a ContactPicker project that includes a ContentPickerTester Activity.
II.
Create a new layout named contentpickertester.xml.
a. Add TextView with ID selected_contact_textview
b. Add a button with ID pick_contact_button and text “Pick Contact”
Modify ContentPickerTester activity.
a. Set this activity to implement onClickListener.
b. Add the constant in ContentPickerTester fields:
public static final int PICK_CONTACT = 1;
c. Add a Button field with the name “button”.
d. Modify the onCreate method:
i. Set the content view to contentpicktester
ii. You have to do something with the Button object that you added as a field… I will not
tell you what you have to do!
iii. Add a click listener for the button, so that it implicitly starts a new activity by specifying
the ACTION_PICK and the contact database URI(content://contacts/)
e. Implement the OnClick method:
III.
What exactly did you do here? You do not understand?? Search and learn about intents!!! Find
an explanation for what you just implemented… I will ask you soon…
Hints: if you do not know what URI is check this source:
http://www.nczonline.net/blog/2009/10/27/data-uris-explained/
Or do your own search
IV.
When ContentPickerTester activity returns, use the result to populate the TextView with the selected
contact’s name:
1
What did you just do?? Make sure you lookup any unknown class or method because… I will ask you
some interesting questions!
What is this Cursor? What does the onActivityResult do?
V.
You will also need to add a READ_CONTACTS permission to allow the app to access contacts:
References:
Professional Android 2.0 Application development, Wrox Programmer to Programmer
PART 2 (if time permits)
VI.
VII.
VIII.
Modify main.xml
a. Include a single ListView with ID contactListView in main.xml
b. Delete the editText box in main.xml
Create a new listitemlayout.xml that includes a single TextView. The id of the textview
should be: itemTextView. Set the TextView width to fill_parent.
Modify ContactPickerActivity
a. Create a new intent
b. Extract the datapath from this intent using method Intent.getData
c. Create a new data URI field: Uri data
for the people stored in the contact list, and bind it to the ListView using
SimpleCursorArrayAdapter
i. Info about SimpleCursorAdapter
http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
d. Create a new Cursor field: Cursor c
2
e.
f.
g.
IX.
Use the code:
To create a SimpleCursorAdapter.
Create a ListView field lv, find the ListView by its ID, and set the adapter of the lv to adapter.
Add an onItemClickListener to the ListView. Selecting a contact from the list should
return a path to the item to the calling Activity.
i. In the onItemClick method first move the cursor to the selected item
ii. Then extract the row id
iii. Finally construct the result URI
Modify the application manifest:
a. Replace the intent-filter tag on the Activity to add support for ACTION_PICK action on
contact data. Replace the Action MAIN with Action PICK
b. Replace the category LAUNCHER with DEFAULT
c. Add a data tag with fields:
android:path="contacts" android:scheme="content"
i.
3
Download