Android api:http://developer.android.com/reference/android/content/intent.html
Contactpicker: A user can select a contact.
1. Create Contactpicker activities (activity)
Location: java->package-> Contactpicker.java
Package Mzx.spike.contactpicker.app;
Import android.app.Activity;
Import Android.content.ContentUris;
Import android.content.Intent;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.provider.ContactsContract;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.ListView;
Import Android.widget.SimpleCursorAdapter;
/** * Created by C.l.wang on 14-3-20. */public class Contactpicker extends activity {@Override public void onCreate (Bundle savedinstancestate
) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main); Final Cursor C = getcontentresolver (). query (ContactsContract.Contacts.CONTENT_URI, NULL, NULL, NULL, NU
ll);
String[] from = new String[]{contactscontract.contacts.display_name_primary};
Int[] to = new Int[]{r.id.itemtextview};
Simplecursoradapter adapter = new Simplecursoradapter (this, r.layout.listitemlayout, C, from, to, 0);
ListView LV = (ListView) Findviewbyid (R.id.contactlistview);
Lv.setadapter (adapter); Lv.setonitemclicklistener (New Listview.onitemclicklistener () {public void Onitemclick (Adapterview<?> p
Arent, view view, int POS, long id) {c.movetoposition (POS);
int rowId = C.getint (C.getcolumnindexorthrow ("_id"));
Uri Outuri = Contenturis.withappendedid (ContactsContract.Contacts.CONTENT_URI, rowId);
Intent outdata = new Intent ();
Outdata.setdata (Outuri);
Setresult (ACTIVITY.RESULT_OK, outdata);
Finish ();
}
}); }
}
Detailed
1. Create an activity to inherit activities, display Activity_main interface;
2. Create a cursor cursor traverse the contacts stored in the contact list;
3. Construct a simple Leng adapter (simplecursoradapter), note that this method is marked as abandonment, followed by the addition of parameter 0, version sdk11+;
4. Bind the adapter to ListView, Setadapter ();
5. Add Item click event (setonitemclicklistener), cursor transplant click Position, remove rowID, construct URI, bind URI to intent;
6. Return results, Setresult (), Result codes (ACTIVITY_RESULT_OK) and Intent;
2. Modify Activity_main.xml
Location: Res->layout->activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "Match_ Parent "
android:layout_height=" match_parent "
android:paddingleft=" @dimen/activity_horizontal_margin "
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical _margin "
android:paddingbottom=" @dimen/activity_vertical_margin "
>
<listview android:id=" @+id /contactlistview "
android:layout_width=" match_parent "
android:layout_height=" wrap_content "
/>"
</RelativeLayout>
Add ListView, Contactpicker will call this layout (layout);
3. Create Listitemlayout.xml
Location: Res->layout->listitemlayout.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" match_parent "
android:layout_" height= "Match_parent"
>
<textview
android:id= "@+id/itemtextview"
Match_parent "
android:layout_height=" wrap_content "
android:padding=" 10DP "
android:textsize=" 16DP "
android:textcolor=" #F0F "
/>
</LinearLayout>
TextView displays each contact in the ListView, Contactpicker calls this layout (layout);
4. Create a test layout contactpickertester.xml
Location: Res->layout->contactpickertester.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" match_parent "
android:layout_" height= "Match_parent"
>
<textview
android:id= "@+id/selected_contact_textview"
android: Layout_width= "Match_parent"
android:layout_height= "wrap_content"
/>
<button
android: Id= "@+id/pick_contact_button"
android:layout_width= "match_parent"
android:layout_height= "WRAP_" Content "
android:text=" Pick contact "
/>
</LinearLayout>
TextView displays selected contacts, button selection buttons, start interface;
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/