Android gets the last activity return value method _android

Source: Internet
Author: User
Tags stub

Activity A and B

A Get Data Activity B returns the activity of the data

Click on the button on a, show the contact list in B on the textview of a and use the selected data to Baseadapter

1: In the main configuration file to declare bactivity and registration address Book read and Write permissions

[HTML]

Copy Code code as follows:

<span style= "FONT-SIZE:18PX;" > <!--read/write access to registered address Book-->
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
<uses-permission android:name= "Android.permission.WRITE_CONTACTS"/>

lt;! The--. Represents the value of the package property in the manifest label above-->
<activity
Android:name= ". Demoactivity "
android:label= "Select Contact" >
</activity></span>

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

[HTML]

Copy Code code 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 a second contact"/>

</LinearLayout>

3: Add a ListView to the activity layout file of B

[HTML]

Copy Code code 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 2 button click event, should be to go back to the last activity returned value, so jump to use the Startactivityforresult () method to activate the activity that needs to return data, and rewrite the Onactivityresult () method to receive the returned data

[Java]

Copy Code code 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 for the first button
*
* @param view
*/
public void Click (View view) {
Intent Intent = new Intent (this, demoactivity.class);
StartActivity (Intent);
Startactivityforresult (Intent, 1);//Request Code
Used to differentiate the requested data, if there is only one request (button), this code can be 0, can not consider his value
}

/**
* Click event for 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
/**
* Call this method when the activity of the jump is used 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 2 buttons, the difference is to trigger the Click event of the button, and then put the returned data in the corresponding EditText
Textview.settext (name);
else if (Requestcode = 2) {
Textview2.settext (name);
}
}

}

}

5: Set B in B's layout file, and go back to his listview, use Baseadapter to add contact data to ListView

[Java]

Copy Code code 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.jb51.net
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.demo);
ListView = (ListView) Findviewbyid (r.id.lv);//Initialize Control
data = Getallcontacts ();//Get all contact names
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 return value
Setresult (0, intent);//Put the return value and add a code to facilitate distinguishing the returned data
Finish ()//end the current activity, equal to click the return button
}

});
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

/**
* Inner class, add data for ListView, form 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;//returns the position of the current list
}

@Override
public long getitemid (int position) {
TODO auto-generated Method Stub
Return position;//returns the current list position
}

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
TextView TV = new TextView (demoactivity.this);
Tv.settextsize (25);//Set the size of the display text,
Tv.settextcolor (color.red);//Set the color of the display text
Tv.settext (Data.get (position))//Set contact data in the corresponding location
return TV;
}

}

/**
* Get the names of all contact persons
*
* @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, NULL, NULL, NULL);
while (Cursor.movetonext ()) {
String name = cursor.getstring (Cursor.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME));
List.add (name);
}
Cursor.close ();
return list;
}
}

Note that the code in the Requestcode in the Startactivityforresult in a and in the Setresult in B is not corresponding, the code in a distinguishes the request space, and the code in B is used to differentiate 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.