android gets the last activity return value
- 2020-05-17 06:24:40
- OfStack
activity A and B
A gets data from activity B returns data from activity
Click the button on A to display the selected data from B's contact list on A's textview for baseadapter
1: declare read and write access to Bactivity and the registered address book in the master configuration file
[html]
<span style="font-size:18px;"> <!-- Register access to the address book -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
lt;!-- . The above said manifest In the label package The value of the attribute -->
<activity
android:name=".DemoActivity"
android:label=" Select contacts " >
</activity></span>
2. Declare 2 button and 2 edittext in the layout file of A and register click events for button
[html]
<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=" choose 1 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 first 2 A contact " />
</LinearLayout>
3: add an listview to the activity layout file of B
[html]
<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 two buttons, because it is to return the value returned by 1 activity, so when you jump again, you should use startActivityForResult() method to activate activity that needs to return data, and rewrite onActivityResult() method to receive the returned data
[java]
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);
}
/**
* The first 1 Button click event
*
* @param view
*/
public void click(View view) {
Intent intent = new Intent(this, DemoActivity.class);
// startActivity(intent);
startActivityForResult(intent, 1);// Request code
// The data used to distinguish the request , If only 1 A request ( button ), this code Can I do for 0, I don't care what his value is
}
/**
* The first 2 Button click event
*
* @param view
*/
public void click2(View view) {
Intent intent = new Intent(this, DemoActivity.class);
// startActivity(intent);
startActivityForResult(intent, 2);// Request code
}
@Override
/**
* When the jump activity( activated activity) After use , This method is called when 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 A button , So distinguish between a click that triggers that button , Then put the returned data into the corresponding EditText In the
textView.setText(name);
} else if (requestCode == 2) {
textView2.setText(name);
}
}
}
}
5: set B's layout file in B, go back to his listview, and use baseadapter to add contact data to listview
[java]
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.ofstack.com
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
listView = (ListView) findViewById(R.id.lv);// Initialize the controller
data = getAllContacts();// Get all the 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 in the return value
setResult(0, intent);// Put in the returned value , And add 1 a Code, Easy to distinguish the returned data
finish();// End of current activity, Is equal to clicking the back button
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* The inner class , for listview Add data , Make a contact list
*
* @author w
*
*/
public class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();// return listview The total length of the
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;// Returns the location 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);// Sets the size of the display text ,
tv.setTextColor(Color.RED);// Sets the color of the displayed text
tv.setText(data.get(position));// Set the contact data in the corresponding location
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, 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 of requestCode in startActivityForResult in A and resultCode in setResult in B are not corresponding. code in A is used to distinguish the request space, and Code in B is used to distinguish the return value