Enable a new activity to obtain its return value. The activity gets the return value.

Source: Internet
Author: User

Enable a new activity to obtain its return value. The activity gets the return value.

1. Start Interface

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = ". mainActivity "> <LinearLayout android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: orientation =" horizontal "> <EditText android: id = "@ + id/et_number" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: hint = "enter a contact"/> <Button android: onClick = "click" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Contact"/> </LinearLayout> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <EditText android: id = "@ + id/et_number2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: hint = "enter a contact"/> <Button android: onClick = "click2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Contact 2"/> </LinearLayout>

2. enable the new activity code.

1 package com. example. smssender; 2 3 import android. OS. bundle; 4 import android. app. activity; 5 import android. content. intent; 6 import android. view. menu; 7 import android. view. view; 8 import android. widget. editText; 9 10 public class MainActivity extends Activity {11 12 private EditText et_number; 13 private EditText et_number2; 14 @ Override15 protected void onCreate (Bundle savedInstanceState) {16 s Uper. onCreate (savedInstanceState); 17 setContentView (R. layout. activity_main); 18 et_number = (EditText) findViewById (R. id. et_number); 19 et_number2 = (EditText) findViewById (R. id. et_number2); 20} 21 22 public void click (View view) {23 Intent intent = new Intent (this, ContactActivity. class); 24 // startActivity (intent); 25 // The request code is used to identify who initiated the request 26 startActivityForResult (intent, 1 ); 27} 28 29 public void click2 (View view) {30 Intent intent = new Intent (this, ContactActivity. class); 31 // startActivity (intent); 32 // The request code is used to identify who initiated the request 33 startActivityForResult (intent, 2 ); 34} 35 36 37 @ Override38 protected void onActivityResult (int requestCode, int resultCode, Intent data) {39 // TODO Auto-generated method stub40 super. onActivityResult (requestCode, resultCode, data); 41 if (data! = Null) {42 String number = data. getStringExtra ("number"); 43 if (requestCode = 1) {44 et_number.setText (number); 45} else {46 et_number2.setText (number ); 47} 48} 49} 50 51}

3. Get contacts

1) inventory file

<Uses-permission android: name = "android. permission. READ_CONTACTS"/> // permission

2) Get a contact through the content provider

1 package com. example. smssender; 2 3 import java. util. arrayList; 4 import java. util. list; 5 6 import android. content. contentResolver; 7 import android. content. context; 8 import android. database. cursor; 9 import android.net. uri; 10 11 public class ContactService {12 public static List <contactInfo> getContactAll (Context context) {13 List <contactInfo> infos = new ArrayList <contactInfo> (); 14 // get the contact through the content provider Contact 15 ContentResolver resolver = context. getContentResolver (); 16 Uri uri = Uri. parse ("content: // com. android. contacts/raw_contacts "); 17 Uri dataUri = Uri. parse ("content: // com. android. contacts/data "); 18 Cursor cursor = resolver. query (uri, null, null); 19 while (cursor. moveToNext () {20 String id = cursor. getString (cursor. getColumnIndex ("contact_id"); 21 Cursor datacursor = resolver. query (dat AUri, null, "raw_contact_id =? ", New String [] {id}, null); 22 contactInfo info = new contactInfo (); 23 while (datacursor. moveToNext () {24 String data1 = datacursor. getString (datacursor. getColumnIndex ("data1"); 25 String mimetype = datacursor. getString (datacursor. getColumnIndex ("mimetype"); 26 if ("vnd. android. cursor. item/name ". equals (mimetype) {27 info. setUsername (data1); 28} else if ("vnd. android. cursor. item/phone_v2 ". equals (mimetype) {29 info. setNumber (data1); 30} 31} 32 33 infos. add (info); 34 35} 36 return infos; 37} 38}

4. Set the contact to go to Listview

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6      7     <ListView 8         android:layout_width="match_parent" 9         android:layout_height="match_parent" 10         android:id="@+id/lv_contact"11         ></ListView>12 13 </LinearLayout>
 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6      7     <TextView  8         android:id="@+id/et_username" 9         android:layout_width="match_parent"10         android:layout_height="wrap_content"11         />12     <TextView 13         android:id="@+id/et_number"14         android:layout_width="match_parent"15         android:layout_height="wrap_content"16         />17 18 </LinearLayout>

Java code:

1 package com. example. smssender; 2 3 import java. util. list; 4 5 import android. app. activity; 6 import android. content. contentResolver; 7 import android. content. intent; 8 import android. database. cursor; 9 import android.net. uri; 10 import android. OS. bundle; 11 import android. view. view; 12 import android. view. viewGroup; 13 import android. widget. adapterView; 14 import android. widget. adapterView. onItemClick Listener; 15 import android. widget. baseAdapter; 16 import android. widget. listView; 17 import android. widget. textView; 18 19 public class ContactActivity extends Activity {20 21 private ListView lv_contact; 22 private List <contactInfo> infos = null; 23 @ Override24 protected void onCreate (Bundle savedInstanceState) {25 26 // TODO Auto-generated method stub27 super. onCreate (savedInstanceState); 28 setConten TView (R. layout. activity_contact); 29 30 infos = ContactService. getContactAll (this); 31 32 lv_contact = (ListView) findViewById (R. id. lv_contact); 33 lv_contact.setAdapter (new ContactAdapter (); 34 35 lv_contact.setOnItemClickListener (new OnItemClickListener () {36 37 @ Override38 public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2, 39 long arg3) {40 // TODO Auto-generated method stub41 contactInfo info = infos. get (arg2); 42 String number = info. getNumber (); 43 Intent data = new Intent (); 44 data. putExtra ("number", number); 45 setResult (0, data); 46 // close the current activity and send the data back to its activator 47 finish (); 48 49} 50 51}); 52} 53 54 private class ContactAdapter extends BaseAdapter {55 56 @ Override57 public int getCount () {58 // TODO Auto-generated method stub59 return infos. size (); 60} 61 62 @ Override63 public Object getItem (int arg0) {64 // TODO Auto-generated method stub65 return null; 66} 67 68 @ Override69 public long getItemId (int arg0) {70 // TODO Auto-generated method stub71 return 0; 72} 73 74 @ Override75 public View getView (int arg0, view arg1, ViewGroup arg2) {76 // TODO Auto-generated method stub77 contactInfo info = infos. get (arg0); 78 View view = View. inflate (ContactActivity. this, R. layout. contact_item, null); 79 TextView et_username = (TextView) view. findViewById (R. id. et_username); 80 et_username.setText (info. getUsername (); 81 82 TextView et_number = (TextView) view. findViewById (R. id. et_number); 83 et_number.setText (info. getNumber (); 84 85 return view; 86} 87 88} 89 90}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.