bean类Contact:
1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class Contact { 5 private String name; 6 private String email; 7 private String address; 8 private List<String> phone = new ArrayList<String>(); 9 10 public Contact() { 11 super(); 12 // TODO Auto-generated constructor stub 13 } 14 15 public Contact(String name, String email, String address, List<String> phone) { 16 super(); 17 this.name = name; 18 this.email = email; 19 this.address = address; 20 this.phone = phone; 21 } 22 23 public String getName() { 24 return name; 25 } 26 27 public void setName(String name) { 28 this.name = name; 29 } 30 31 public String getEmail() { 32 return email; 33 } 34 35 public void setEmail(String email) { 36 this.email = email; 37 } 38 39 public String getAddress() { 40 return address; 41 } 42 43 public void setAddress(String address) { 44 this.address = address; 45 } 46 47 public List<String> getPhone() { 48 return phone; 49 } 50 51 public void setPhone(List<String> phone) { 52 this.phone = phone; 53 } 54 }
Contact
读取联系人:
1 import java.util.ArrayList; 2 import java.util.List; 3 4 import android.app.Activity; 5 import android.content.ContentResolver; 6 import android.database.Cursor; 7 import android.net.Uri; 8 import android.os.Bundle; 9 import android.util.Log; 10 public class MainActivity extends Activity { 11 private static final String TAG = "MainActivity"; 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 readContact(); 17 } 18 19 public List<Contact> readContact() { 20 List<Contact> list ; 21 ContentResolver resolver = getContentResolver(); 22 Uri raw_contactUri = Uri.parse("content://com.android.contacts/raw_contacts"); 23 Cursor cursor = resolver.query(raw_contactUri, new String[] { "contact_id" }, 24 null, null, null); 25 if (cursor != null && cursor.getCount() > 0) { 26 list= new ArrayList<Contact>(); 27 while (cursor.moveToNext()) { 28 Integer contact_id = cursor.getInt(0); 29 Uri dataUri = Uri.parse("content://com.android.contacts/data"); 30 String selection = "raw_contact_id=?"; 31 String[] selectionArgs = new String[] { String.valueOf(contact_id) }; 32 Cursor cursor_data = resolver.query(dataUri, new String[] { "mimetype", 33 "data1" }, selection, 34 selectionArgs, null); 35 if (cursor_data != null && cursor_data.getCount() > 0) { 36 Contact contact = new Contact(); 37 while (cursor_data.moveToNext()) { 38 String mimetype = cursor_data.getString(0); 39 String data1 = cursor_data.getString(1); 40 if ("vnd.android.cursor.item/name".equals(mimetype)) { 41 contact.setName(data1); 42 } else if ("vnd.android.cursor.item/postal-address_v2".equals(mimetype)) { 43 contact.setAddress(data1); 44 } else if ("vnd.android.cursor.item/email_v2".equals(mimetype)) { 45 contact.setEmail(data1); 46 } else if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) { 47 contact.getPhone().add(data1); 48 } 49 list.add(contact); 50 Log.i(TAG, "mimetype=" + mimetype + "\tdata1=" + data1); 51 } 52 cursor_data.close(); 53 } 54 } 55 cursor.close(); 56 Log.i(TAG, "----------"); 57 return list; 58 } 59 return null; 60 } 61 62 }
readContact
添加联系人:
public void writeContact() { //1.获得访问内容提供者的工具类 ContentResolver resolver = getContentResolver(); //2. 获取到已存入联系人数据库里面的最后一个联系人的contact_id,然后在加1 就是现在要添加的练习题的contact_id Uri raw_contact_uri = Uri.parse("content://com.android.contacts/raw_contacts"); Cursor cursor = resolver.query(raw_contact_uri, new String[]{"contact_id"}, null, null, "contact_id desc limit 1"); Integer max_contact_id = 0; if(null != cursor && cursor.moveToFirst()) { max_contact_id = cursor.getInt(0); } max_contact_id = max_contact_id + 1; //3. 往raw_contact表里面添加contact_id ContentValues values = new ContentValues(); values.put("contact_id", max_contact_id); raw_contact_uri = resolver.insert(raw_contact_uri, values); Log.i(TAG, "raw_contact_uri=" + raw_contact_uri); //4. 往data里面添加数据(姓名,email,address,phone等信息) //注意:一个信息是一样记录,所以添加一个联系人的多个信息的时候要执行多个insert操作 Uri data_uri = Uri.parse("content://com.android.contacts/data"); values.clear(); //复用之前一定要清空否则会影响下面的插入操作 values.put("raw_contact_id", max_contact_id); values.put("mimetype", "vnd.android.cursor.item/name"); values.put("data1", "1505"); resolver.insert(data_uri, values); values.clear(); //复用之前一定要清空否则会影响下面的插入操作 values.put("raw_contact_id", max_contact_id); values.put("mimetype", "vnd.android.cursor.item/email_v2"); values.put("data1", "[email protected],com"); resolver.insert(data_uri, values); values.clear(); //复用之前一定要清空否则会影响下面的插入操作 values.put("raw_contact_id", max_contact_id); values.put("mimetype", "vnd.android.cursor.item/phone_v2"); values.put("data1", "16824524"); resolver.insert(data_uri, values); values.clear(); //复用之前一定要清空否则会影响下面的插入操作 values.put("raw_contact_id", max_contact_id); values.put("mimetype", "vnd.android.cursor.item/postal-address_v2"); values.put("data1", "西安路"); resolver.insert(data_uri, values); }
writeContact
时间: 2024-11-08 03:41:11