从新的activity取得返回值
来源:互联网 发布:公安情报数据库 编辑:程序博客网 时间:2024/05/17 01:55
<!-- 读取联系人信息权限 --><uses-permission android:name="android.permission.READ_CONTACTS"/>package com.example.send;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity implements OnClickListener {private EditText et_contact;private EditText et_contact2;private Button bu_contact_number;private Button bu_contact_number2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_contact = (EditText) findViewById(R.id.et_contact);et_contact2 = (EditText) findViewById(R.id.et_contact2);bu_contact_number = (Button) findViewById(R.id.bu_contact_number);bu_contact_number.setOnClickListener(this);bu_contact_number = (Button) findViewById(R.id.bu_contact_number2);bu_contact_number.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.bu_contact_number://设置启动的activity,请求码为1Intent intent = new Intent(this, SelectContactActivity.class);startActivityForResult(intent, 1);break;case R.id.bu_contact_number2://设置启动的activity,请求码为2Intent intent2 = new Intent(this, SelectContactActivity.class);startActivityForResult(intent2, 2);break;}}//这个方法为当前activity开启另一个新的activity之后,在新的activity关闭的时候调用的方法@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {//取得返回的数据if (data != null) {String number = data.getStringExtra("number");//如果点击事件请求码为1,把数据设置到第一个编辑框内if (requestCode == 1) {System.out.println("bug-----8");et_contact.setText(number);//如果点击事件请求码为2,把数据设置到第二个编辑框内} else if (requestCode == 2) {System.out.println("bug-----7");et_contact2.setText(number);}}super.onActivityResult(requestCode, resultCode, data);}}package com.example.send;import java.util.List;import android.app.Activity;import android.content.Intent;import android.os.Bundle;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;import com.example.send.domain.ContactInfo;public class SelectContactActivity extends Activity {private ListView lv_select_contact;private List<ContactInfo> contactInfos;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.lv_select_contact);lv_select_contact = (ListView) findViewById(R.id.lv_select_contact);// 内容提供者查询出来返回联系人的集合,也就是所有联系人的数据contactInfos = ContactService.getContactInfos(this);lv_select_contact.setAdapter(new ContactAdapter());//设置在listview里面点击一个条目的点击事件lv_select_contact.setOnItemClickListener(new OnItemClickListener(){@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {ContactInfo info = contactInfos.get(position);String number = info.getNumber();Intent data = new Intent();data.putExtra("number", number);setResult(0, data);//结束当前界面,把数据带回给原来的界面finish();}});}private class ContactAdapter extends BaseAdapter {@Overridepublic int getCount() {//返回集合的大小return contactInfos.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {//设置集合的位置ContactInfo info = contactInfos.get(position);View view = View.inflate(SelectContactActivity.this, R.layout.item_contact, null);TextView tv_name = (TextView) view.findViewById(R.id.tv_name);tv_name.setText(info.getName());TextView tv_number = (TextView) view.findViewById(R.id.tv_number);tv_number.setText(info.getNumber());return view;}}}package com.example.send;import java.util.ArrayList;import java.util.List;import android.content.ContentResolver;import android.content.Context;import android.database.Cursor;import android.net.Uri;import com.example.send.domain.ContactInfo;public class ContactService {public static List<ContactInfo> getContactInfos(Context context) {List<ContactInfo> contactInfos = new ArrayList<ContactInfo>();ContentResolver resolver = context.getContentResolver();//系统内容提供者路径content://com.android.contacts/raw_contacts为存放联系人id路径//系统内容提供者路径content://com.android.contacts/data为存放联系人数据的路径Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");Uri dataUri = Uri.parse("content://com.android.contacts/data");Cursor cursor = resolver.query(uri, null, null, null, null);while (cursor.moveToNext()){//查询的这个表存放联系人id的String id = cursor.getString(cursor.getColumnIndex("contact_id"));if (id != null){//查询的这个表用来存入联系人数据的,以id为标志,一个id,代表一个联系人,一个联系人存放两个数据"data1","mimetype"Cursor dataCursor = resolver.query(dataUri, new String[]{"data1","mimetype"}, "raw_contact_id=?", new String[]{id}, null);//查询的一行的数据有两列,一列为data用来存放数据的(0),另一列为mimetype,用来存放数据为哪一类型的(1)ContactInfo conactInfo = new ContactInfo();while (dataCursor.moveToNext()){//查询表的数据的一列String data = dataCursor.getString(0);//查询表的数据类型的一列String mimetype = dataCursor.getString(1);//如果查询的这一行数据类型为名字,就是把这行的数据加入到一人联系人对象的name中,若为phone)v2,就把电话号码加入到联系人对象的number中if ("vnd.android.cursor.item/name".equals(mimetype)){conactInfo.setName(data);} else if ("vnd.android.cursor.item/phone_v2".equals(mimetype)){conactInfo.setNumber(data);}}//把每次查询出来的一个联系人加入到集合中contactInfos.add(conactInfo);dataCursor.close();}}cursor.close();return contactInfos;}}package com.example.send.domain;public class ContactInfo {private String name;private String number;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}}<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" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="50dip" android:hint="请输入联系人电话号码" /> <Button android:id="@+id/selectContact" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="联系人" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/et_number2" android:layout_width="match_parent" android:layout_height="50dip" android:hint="请输入联系人电话号码" /> <Button android:id="@+id/selectContact2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="第二个联系人" /> </RelativeLayout> <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dip" android:hint="请输短信内容" android:inputType="textMultiLine" android:minLines="5" /> <Button android:id="@+id/sendsms" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送短信.." /></LinearLayout><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/lv_selectcontcat" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView></LinearLayout><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_contact_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="姓名" android:textColor="#000000" android:textSize="18sp" /> <TextView android:id="@+id/tv_contact_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="电话" android:textColor="#990000" android:textSize="16sp" /></LinearLayout>全部代码:package com.example.send;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity implements OnClickListener {private EditText et_contact;private EditText et_contact2;private Button bu_contact_number;private Button bu_contact_number2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_contact = (EditText) findViewById(R.id.et_contact);et_contact2 = (EditText) findViewById(R.id.et_contact2);bu_contact_number = (Button) findViewById(R.id.bu_contact_number);bu_contact_number.setOnClickListener(this);bu_contact_number = (Button) findViewById(R.id.bu_contact_number2);bu_contact_number.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.bu_contact_number:Intent intent = new Intent(this, SelectContactActivity.class);startActivityForResult(intent, 1);break;case R.id.bu_contact_number2:Intent intent2 = new Intent(this, SelectContactActivity.class);startActivityForResult(intent2, 2);break;}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (data != null) {String number = data.getStringExtra("number");if (requestCode == 1) {System.out.println("bug-----8");et_contact.setText(number);} else if (requestCode == 2) {System.out.println("bug-----7");et_contact2.setText(number);}}super.onActivityResult(requestCode, resultCode, data);}}package com.example.send;import java.util.List;import android.app.Activity;import android.content.Intent;import android.os.Bundle;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;import com.example.send.domain.ContactInfo;public class SelectContactActivity extends Activity {private ListView lv_select_contact;private List<ContactInfo> contactInfos;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.lv_select_contact);lv_select_contact = (ListView) findViewById(R.id.lv_select_contact);contactInfos = ContactService.getContactInfos(this);lv_select_contact.setAdapter(new ContactAdapter());lv_select_contact.setOnItemClickListener(new OnItemClickListener(){@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {ContactInfo info = contactInfos.get(position);String number = info.getNumber();Intent data = new Intent();data.putExtra("number", number);setResult(0, data);finish();}});}private class ContactAdapter extends BaseAdapter {@Overridepublic int getCount() {return contactInfos.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ContactInfo info = contactInfos.get(position);View view = View.inflate(SelectContactActivity.this, R.layout.item_contact, null);TextView tv_name = (TextView) view.findViewById(R.id.tv_name);tv_name.setText(info.getName());TextView tv_number = (TextView) view.findViewById(R.id.tv_number);tv_number.setText(info.getNumber());return view;}}}package com.example.send;import java.util.ArrayList;import java.util.List;import android.content.ContentResolver;import android.content.Context;import android.database.Cursor;import android.net.Uri;import com.example.send.domain.ContactInfo;public class ContactService {public static List<ContactInfo> getContactInfos(Context context) {List<ContactInfo> contactInfos = new ArrayList<ContactInfo>();ContentResolver resolver = context.getContentResolver();Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");Uri dataUri = Uri.parse("content://com.android.contacts/data");Cursor cursor = resolver.query(uri, null, null, null, null);while (cursor.moveToNext()){String id = cursor.getString(cursor.getColumnIndex("contact_id"));if (id != null){Cursor dataCursor = resolver.query(dataUri, new String[]{"data1","mimetype"}, "raw_contact_id=?", new String[]{id}, null);ContactInfo conactInfo = new ContactInfo();while (dataCursor.moveToNext()){String data = dataCursor.getString(0);String mimetype = dataCursor.getString(1);if ("vnd.android.cursor.item/name".equals(mimetype)){conactInfo.setName(data);} else if ("vnd.android.cursor.item/phone_v2".equals(mimetype)){conactInfo.setNumber(data);}}contactInfos.add(conactInfo);dataCursor.close();}}cursor.close();return contactInfos;}}package com.example.send.domain;public class ContactInfo {private String name;private String number;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}}<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" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="50dip" android:hint="请输入联系人电话号码" /> <Button android:id="@+id/selectContact" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="联系人" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/et_number2" android:layout_width="match_parent" android:layout_height="50dip" android:hint="请输入联系人电话号码" /> <Button android:id="@+id/selectContact2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="第二个联系人" /> </RelativeLayout> <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dip" android:hint="请输短信内容" android:inputType="textMultiLine" android:minLines="5" /> <Button android:id="@+id/sendsms" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送短信.." /></LinearLayout><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/lv_selectcontcat" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView></LinearLayout><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_contact_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="姓名" android:textColor="#000000" android:textSize="18sp" /> <TextView android:id="@+id/tv_contact_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="电话" android:textColor="#990000" android:textSize="16sp" /></LinearLayout>
- 从新的activity取得返回值
- 【从头学android】从新的Activity中返回数据给前一个Activity
- HQL的返回值该怎么取得?
- HQL的返回值该怎么取得?
- 取得Hibernate返回对象的列值
- 可以取得返回值的线程
- HQL的返回值该怎么取得?
- Activity的生命周期,状态保存,窗口显示,取得返回结果,安全退出
- 得到Activity的返回值
- 取得正在运行的Activity
- service--取得运行的Activity
- Hibernate的HQL的该怎么取得返回值
- JDBC 插入时, 取得插入的主键的返回值
- 在.net中,取得储存过程返回值的方法
- C#中取得存储过程的返回值
- Enterprise Library 2.0 取得存储过程的返回值
- Shell中如何取得Procedure的返回值
- 用变量来取得exec('')的标量返回值.
- hdu 1072 Nightmare
- 通过 U 盘安装 Windows Server 2008
- Github上新建一个repository
- 动态能量控制(for NS2)
- 临界区与互斥时间
- 从新的activity取得返回值
- 如何使用git回退部分修改
- 在SharePoint 2010中创建联系人Web数据库网站
- Single Number--找出数组中唯一的一个只出现一次的元素
- Project中如何正确导入第三方jar包
- Linux el6安装MySQL-5.6
- 生产者消费者问题VC实现
- 利用urlrewriter实现页面伪静态
- 字符指针与字符数组真正的区别