Android gets the returned value of the previous activity

Source: Internet
Author: User

Activity A and B

A. activity B that obtains data returns the activity of the data.

Click the button on A to display the selected data in the contact list of B in textview OF A using baseadapter.

1: declare the read and write permissions of Bactivity and registration address book in the main configuration file

[Html]

Copy codeThe Code is as follows: <span style = "font-size: 18px;"> <! -- Register the read and write permissions of the address book -->
<Uses-permission android: name = "android. permission. READ_CONTACTS"/>
<Uses-permission android: name = "android. permission. WRITE_CONTACTS"/>

Lt ;! --. Indicates the value of the package attribute in the preceding manifest tag -->
<Activity
Android: name = ". DemoActivity"
Android: label = "select contact">
</Activity> </span>

2. Declare 2 buttons and 2 edittext in the layout file of A and register the click event for the button.

[Html]

Copy codeThe Code is as follows: <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">

<EditText
Android: id = "@ + id/textView"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>

<Button
Android: id = "@ + id/button1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: onClick = "click"
Android: text = "select a contact"/>

<EditText
Android: id = "@ + id/textView2"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>

<Button
Android: id = "@ + id/button2"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: onClick = "click2"
Android: text = "select the second contact"/>

</LinearLayout>

3: Add a listview to the activity layout file of B.

[Html]

Copy codeThe Code is as follows: <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">

<ListView
Android: id = "@ + id/lv"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
</ListView>

</LinearLayout>

4: Get textView in a, and write the Click Event of the two buttons. It should be the value returned by the previous activity, so startActivityForResult () should be used for redirection () to activate the activity that needs to return data, and override the onActivityResult () method to receive the returned data.

[Java]

Copy codeThe Code is as follows: package com. example. getresultdata;

Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. Menu;
Import android. view. View;
Import android. widget. TextView;

Public class MainActivity extends Activity {
Private TextView textView;
Private TextView textView2;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TextView = (TextView) findViewById (R. id. textView );
TextView2 = (TextView) findViewById (R. id. textView2 );
}

/**
* Click event of the first button
*
* @ Param view
*/
Public void click (View view ){
Intent intent = new Intent (this, DemoActivity. class );
// StartActivity (intent );
StartActivityForResult (intent, 1); // Request Code
// Identifies the request data. If there is only one request (button), the code can be 0, and its value can be ignored.
}

/**
* Click event of the second button
*
* @ Param view
*/
Public void click2 (View view ){
Intent intent = new Intent (this, DemoActivity. class );
// StartActivity (intent );
StartActivityForResult (intent, 2); // Request Code
}

@ Override
/**
* This method is called when the redirected activity (activated activity) is used up and destroyed.
*/
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
// TODO Auto-generated method stub
Super. onActivityResult (requestCode, resultCode, data );
If (data! = Null ){
String name = data. getStringExtra ("name ");
If (requestCode = 1) {// because there are two buttons, you need to distinguish the event that triggers the button and then put the returned data in the corresponding EditText
TextView. setText (name );
} Else if (requestCode = 2 ){
TextView2.setText (name );
}
}

}

}

5: Set the layout file of B in B, go back to its listview, and use baseadapter to add contact data to listview.

[Java]

Copy codeThe Code is as follows: package com. example. getresultdata;

Import java. util. ArrayList;
Import java. util. List;

Import android. app. Activity;
Import android. content. ContentResolver;
Import android. content. Intent;
Import android. database. Cursor;
Import android. graphics. Color;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. provider. ContactsContract;
Import android. view. Menu;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemClickListener;
Import android. widget. BaseAdapter;
Import android. widget. ListView;
Import android. widget. TextView;

Public class DemoActivity extends Activity {
Private ListView listView;
Private List <String> data;

@ Override www.2cto.com
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. demo );
ListView = (ListView) findViewById (R. id. lv); // initialize the control
Data = getAllContacts (); // obtain the names of all contacts
ListView. setAdapter (new MyAdapter ());
ListView. setOnItemClickListener (new OnItemClickListener (){

@ Override
Public void onItemClick (AdapterView <?> Parent, View view, int position, long id ){
TextView textView = (TextView) view;
String name = textView. getText (). toString ();
Intent intent = new Intent ();
Intent. putExtra ("name", name); // put the returned value
SetResult (0, intent); // enter the return value and add a Code to distinguish the returned data.
Finish (); // ends the current activity. It is equal to clicking the return button.
}

});
}

@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}

/**
* Internal class: add data for listview to form a contact list
*
* @ Author w
*
*/
Public class MyAdapter extends BaseAdapter {

@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return data. size (); // returns the total length of the listview.
}

@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return position; // return the position of the current list
}

@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position; // return the position of the current list
}

@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
TextView TV = new TextView (DemoActivity. this );
TV. setTextSize (25); // you can specify the size of the displayed text,
TV. setTextColor (Color. RED); // you can specify the Color of the displayed text.
TV. setText (data. get (position); // set the contact data at the corresponding position
Return TV;
}

}

/**
* Get the names of all contacts
*
* @ Return
*/
Private List <String> getAllContacts (){
List <String> list = new ArrayList <String> ();
// Or uri = ContactsContract. Contacts. CONTENT_URI
Uri uri = Uri. parse ("content: // com. android. contacts/contacts ");
ContentResolver resolver = this. getContentResolver ();
Cursor cursor = resolver. query (uri, null );
While (cursor. moveToNext ()){
String name = cursor. getString (cursor. getColumnIndex (ContactsContract. Contacts. DISPLAY_NAME ));
List. add (name );
}
Cursor. close ();
Return list;
}
}

Note that the requestCode in startActivityForResult in a does not correspond to the resultCode in setResult in B. The code in A distinguishes the request space, and the code in B distinguishes the return value.

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.