知识点:
1 使用Intent在不同的Activity间传递数据
1 使用Intent在不同的Activity间传递数据
1 在编号为A的Activity中使用startActivityForResult开启编号为B的Activity
startActivityForResult(new Intent(SetupActivity3.this,ReadContactActivity.class),0);
2 在编号为B的Activity中给Intent添加数据;
Intent intent = new Intent(); intent.putExtra("phone",listContact.get(position).contactphone); setResult(0,intent);
3 在编号为A的onActivityResult中得到返回数据
@Override protected void onActivityResult(int requestCode,int resultCode,Intent data) { System.out.println(" resultCode : "+resultCode); String phonenum =data.getStringExtra("phone"); et_choice_contact.setText(phonenum); super.onActivityResult(requestCode,resultCode,data); }
2 读取sqlite数据库中的联系人数据
1 建立一个联系人信息类
public class ContactInfo { public String contactId ; public String contactname; public String contactphone; }
2 读取数据库中的联系人信息:
package com.example.mobliesoft.ReadLib; import java.util.ArrayList; import java.util.HashMap; import android.content.Context; import android.database.Cursor; import android.net.Uri; public class ReadFormLib { /** * 读取联系人信息 * @return ArrayList<HashMap> */ public ArrayList<ContactInfo> ReadContact(Context context) { ArrayList<HashMap> ListofContact =new ArrayList<HashMap>(); ArrayList<ContactInfo> ListofContact2 =new ArrayList<ContactInfo>(); Uri rawContactsUri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri dataUri = Uri.parse("content://com.android.contacts/data"); // 读取raw_contact 表 Cursor rawContactsCursor = context.getContentResolver().query(rawContactsUri,new String[] { "contact_id","display_name" },null,null); if (rawContactsCursor != null) { while (rawContactsCursor.moveToNext()) { String contactId = rawContactsCursor.getString(0); String contactname = rawContactsCursor.getString(1); //System.out.println(contactId + " ; " + contactname + " ; "); // 读取data 表 注意实际读的是view_data的虚表 Cursor datacursor = context.getContentResolver().query(dataUri,new String[] { "data1","mimetype" },"contact_id=?",new String []{contactId},null); HashMap<String,String> Contactpeople=new HashMap<String,String>(); ContactInfo contactInfo = new ContactInfo(); if (datacursor != null) { while (datacursor.moveToNext()) { String data1 = datacursor.getString(0); String minetype = datacursor.getString(1); //System.out.println( contactId+" ; "+data1+" ; "+ minetype); if (minetype.equals("vnd.android.cursor.item/phone_v2")) { Contactpeople.put(contactname,data1.replace("-","").replace(" ","").trim()); contactInfo.contactId=contactId; contactInfo.contactname=contactname; contactInfo.contactphone=data1.replace("-","").trim(); ListofContact.add(Contactpeople); ListofContact2.add(contactInfo); } } } datacursor.close(); } rawContactsCursor.close(); } return ListofContact2; } }