android 獲取上一個(gè)activity返回值的方法
activity A和B
A 獲取數(shù)據(jù)的activity B返回?cái)?shù)據(jù)的activity
點(diǎn)擊A上的按鈕,在A的textview上顯示B中的聯(lián)系人列表選中的數(shù)據(jù) 用到baseadapter
1:在主配置文件中聲明Bactivity 和 注冊(cè)通訊錄的讀寫(xiě)權(quán)限
[html]
<span style="font-size:18px;"> <!-- 注冊(cè)通訊錄的讀寫(xiě)權(quán)限 -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
lt;!-- .表示上面 manifest 標(biāo)簽中 package屬性的值 -->
<activity
android:name=".DemoActivity"
android:label="選擇聯(lián)系人" >
</activity></span>
2.在A的布局文件中聲明2個(gè)button和2個(gè)edittext 并給button注冊(cè)點(diǎn)擊事件
[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="選擇一個(gè)聯(lián)系人" />
<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="選擇第二個(gè)聯(lián)系人" />
</LinearLayout>
3:在B的activity布局文件中添加一個(gè)listview
[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:在A中獲取textView,和編寫(xiě)2個(gè)按鈕的單擊事件,應(yīng)為是要回去上一個(gè)activity返回的值,所以再跳轉(zhuǎn)的時(shí)候要用startActivityForResult()方法來(lái)激活需要返回?cái)?shù)據(jù)的activity,并重寫(xiě)onActivityResult()方法接收返回的數(shù)據(jù)
[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);
}
/**
* 第一個(gè)按鈕的點(diǎn)擊事件
*
* @param view
*/
public void click(View view) {
Intent intent = new Intent(this, DemoActivity.class);
// startActivity(intent);
startActivityForResult(intent, 1);// 請(qǐng)求碼
// 用于區(qū)分請(qǐng)求的數(shù)據(jù),如果只有一個(gè)請(qǐng)求(按鈕),這個(gè)code可以為0,可以不考慮他的值
}
/**
* 第二個(gè)按鈕的點(diǎn)擊事件
*
* @param view
*/
public void click2(View view) {
Intent intent = new Intent(this, DemoActivity.class);
// startActivity(intent);
startActivityForResult(intent, 2);// 請(qǐng)求碼
}
@Override
/**
* 當(dāng)跳轉(zhuǎn)的activity(被激活的activity)使用完畢,銷(xiāo)毀的時(shí)候調(diào)用該方法
*/
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) {// 因?yàn)橛?個(gè)按鈕,所以要區(qū)分是觸發(fā)了那個(gè)按鈕的單擊事件,然后把返回的數(shù)據(jù)放到對(duì)應(yīng)的EditText中
textView.setText(name);
} else if (requestCode == 2) {
textView2.setText(name);
}
}
}
}
5:在B中設(shè)置B的布局文件,并回去他的listview,使用baseadapter給listview添加聯(lián)系人數(shù)據(jù)
[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.dhdzp.com
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
listView = (ListView) findViewById(R.id.lv);// 初始化控件
data = getAllContacts();// 獲取所有的聯(lián)系人姓名
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);// 放入返回值
setResult(0, intent);// 放入回傳的值,并添加一個(gè)Code,方便區(qū)分返回的數(shù)據(jù)
finish();// 結(jié)束當(dāng)前的activity,等于點(diǎn)擊返回按鈕
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* 內(nèi)部類(lèi),為listview添加數(shù)據(jù),構(gòu)成聯(lián)系人列表
*
* @author w
*
*/
public class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();// 返回listview的總長(zhǎng)度
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;// 返回當(dāng)前列表的位置
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;// 返回當(dāng)前列表位置
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(DemoActivity.this);
tv.setTextSize(25);// 設(shè)置顯示文本的大小,
tv.setTextColor(Color.RED);// 設(shè)置顯示文本的顏色
tv.setText(data.get(position));// 在對(duì)應(yīng)的位置設(shè)置聯(lián)系人數(shù)據(jù)
return tv;
}
}
/**
* 獲取所有聯(lián)系人的姓名
*
* @return
*/
private List<String> getAllContacts() {
List<String> list = new ArrayList<String>();
// 或者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;
}
}
注意 在A中的startActivityForResult中的requestCode 和B中的setResult中的resultCode 兩者的code不是對(duì)應(yīng)的,A中的code用區(qū)分請(qǐng)求空間,B中的Code是用以區(qū)分返回值
相關(guān)文章
深入Android中BroadcastReceiver的兩種注冊(cè)方式(靜態(tài)和動(dòng)態(tài))詳解
這篇文章主要介紹了深入Android中BroadcastReceiver的兩種注冊(cè)方式(靜態(tài)和動(dòng)態(tài))詳解,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12
Android判斷網(wǎng)絡(luò)類(lèi)型的方法(2g,3g還是wifi)
這篇文章主要介紹了Android判斷網(wǎng)絡(luò)類(lèi)型的方法,可實(shí)現(xiàn)判斷2g,3g還是wifi的功能,結(jié)合實(shí)例形式分析了Android針對(duì)網(wǎng)絡(luò)類(lèi)型的相關(guān)判定技巧,需要的朋友可以參考下2016-02-02
android socket聊天室功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了android socket聊天室功能實(shí)現(xiàn)方法,不單純是聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Compose?動(dòng)畫(huà)藝術(shù)之屬性動(dòng)畫(huà)探索
這篇文章主要介紹了Compose動(dòng)畫(huà)藝術(shù)之屬性動(dòng)畫(huà)探索,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
android加密參數(shù)定位實(shí)現(xiàn)方法
這篇文章主要介紹了android加密參數(shù)定位方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
基于Android設(shè)計(jì)模式之--SDK源碼之策略模式的詳解
本篇文章介紹了,基于Android設(shè)計(jì)模式之--SDK源碼之策略模式的詳解。需要的朋友參考下2013-04-04
Android手勢(shì)識(shí)別器GestureDetector使用詳解
這篇文章主要為大家詳細(xì)介紹了Android手勢(shì)識(shí)別器GestureDetector的使用方法解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android ListView的item中嵌套ScrollView的解決辦法
有時(shí)候,listview 的item要顯示的字段比較多,考慮到顯示問(wèn)題,item外面不得不嵌套ScrollView來(lái)實(shí)現(xiàn),糾結(jié)怎么解決此問(wèn)題呢?下面小編給大家分享下Android ListView的item中嵌套ScrollView的解決辦法,感興趣的朋友一起看看吧2016-10-10

