1. 视图示例, 按选择弹出 2界面, 选择选项 回显到1
2. 示例代码
MainActivity.java, 第一个activity
public class MainActivity extends Activity {
private EditText et_number;
private EditText et_number2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_number = (EditText) findViewById(R.id.et_number);
et_number2 = (EditText) findViewById(R.id.et_number2);
}
/**
* 激活一个新的界面 让用户选择联系人
*
* @param view
*/
public void selectContact(View view) {
Intent intent = new Intent(this, SelectContactActivity.class);
// 开启一个新的activity
// startActivity(intent);
// 开启一个新的activity 并且获取这个新开启的activity执行完毕后返回的结果
startActivityForResult(intent, 1);
}
/**
* 激活一个新的界面 让用户选择第二个联系人
*
* @param view
*/
public void selectContact2(View view) {
Intent intent = new Intent(this, SelectContactActivity.class);
// 开启一个新的activity
// startActivity(intent);
// 开启一个新的activity 并且获取这个新开启的activity执行完毕后返回的结果
startActivityForResult(intent, 2);
}
// 当新开启的activity 关闭的时候 调用的方法.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("onActivityResult");
if (data != null) {
String number = data.getStringExtra("number");
if (requestCode == 1) {
et_number.setText(number);
}else if(requestCode==2){
et_number2.setText(number);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
SelectContactActivity.java 第二个activity
public class SelectContactActivity extends Activity {
private ListView lv_select_contact;
private List contactInfos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_contact);
lv_select_contact = (ListView) findViewById(R.id.lv_select_contact);
contactInfos = ContactInfoService.getContactInfos(this);
lv_select_contact.setAdapter(new ContactAdapter());
//给listview的条目设置 一下点击事件
lv_select_contact.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int position,
long arg3) {
ContactInfo info = contactInfos.get(position);
String number = info.getNumber();
//传递数据给调用他的activity
Intent data = new Intent();
data.putExtra("number", number);
setResult(100, data);
//关闭当前的activity 然后传递数据给 调用者 调用者就会执行 onactivityResult的方法
finish();
}
});
}
private class ContactAdapter extends BaseAdapter{
@Override
public int getCount() {
return contactInfos.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ContactInfo info = contactInfos.get(position);
View view = View.inflate(getApplicationContext(), R.layout.contact_item, null);
TextView tv_name = (TextView) view.findViewById(R.id.tv_contact_name);
TextView tv_number = (TextView) view.findViewById(R.id.tv_contact_number);
tv_name.setText(info.getName());
tv_number.setText(info.getNumber());
return view;
}
}
}
ContactInfoService.java, 从db获取所有联系人信息
public class ContactInfoService {
/**
* 返回系统所有的联系人的信息
* @return
*/
public static List getContactInfos(Context context){
//创建一个系统所有联系人的集合
List contactInfos = new ArrayList();
ContentResolver resolver = context.getContentResolver();
// raw_contact 表的uri
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
// data 表的uri
Uri dataUri = Uri.parse("content://com.android.contacts/data");
Cursor cursor = resolver.query(uri, new String[] { "contact_id" },
null, null, null);
while (cursor.moveToNext()) {
String id = cursor.getString(0);
if (id != null) {
Cursor dataCursor = resolver.query(dataUri, new String[] {
"data1", "mimetype" }, "raw_contact_id=?",
new String[] { id }, null);
ContactInfo contactInfo = new ContactInfo();
while (dataCursor.moveToNext()) {
String data = dataCursor.getString(0);
String mimetype = dataCursor.getString(1);
if("vnd.android.cursor.item/name".equals(mimetype)){
contactInfo.setName(data);
}else if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
contactInfo.setNumber(data);
}
}
contactInfos.add(contactInfo);
dataCursor.close();
}
}
cursor.close();
return contactInfos;
}
}
ContactInfo.java, 联系人javabean
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;
}
}
activity_main.xml, 第一个activity布局
activity_select_contact.xml, 第二个activity布局
contact_item.xml, ListView布局
AndroidManifest.xml