Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android操作通訊錄的聯系人

android操作通訊錄的聯系人

編輯:關於Android編程

界面配置文件   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent" >     <TextView          android:id="@+id/text"         android:textSize="30px"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:gravity="center"         android:text="手機聯系人列表"/>     <ListView          android:id="@+id/contactslist"         android:layout_width="fill_parent"         android:layout_height="wrap_content"/> </LinearLayout>     SimpleAdapter的配置文件   <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:orientation="horizontal" >     <TextView         android:id="@+id/_id"         android:textSize="30px"         android:layout_width="240px"         android:layout_height="wrap_content"/> <TextView         android:id="@+id/name"         android:textSize="30px"         android:layout_width="240px"         android:layout_height="wrap_content"/> </LinearLayout>     Acitivity程序   package com.example.contactproject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.app.Activity; import android.database.Cursor; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity { private ListView contactslist=null; private SimpleAdapter simpleadapter=null; private Cursor result=null; private List<Map<String, Object>> allContacts=new ArrayList<Map<String,Object>>(); @Override public boolean onContextItemSelected(MenuItem item) {//選中某個菜單項 AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();//取得菜單項 int position=info.position;//取得操作ID String contactsId=this.allContacts.get(position).get("_id").toString();//取得數據ID switch(item.getItemId()){//判斷菜單項ID case Menu.FIRST+1://查詢數據 String phoneSelection=ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?";//設置查詢條件    String[] phoneSelectionAras=new String[]{contactsId}; //查詢參數    Cursor result=super.getContentResolver().query(     ContactsContract.CommonDataKinds.Phone.CONTENT_URI,     null, phoneSelection, phoneSelectionAras, null);//獲得查詢的手機號碼    StringBuffer buf=new StringBuffer();//用於接受號碼    buf.append("電話號碼是:");     for(result.moveToFirst();!result.isAfterLast();result.moveToNext()){//循環取出數據     buf.append(result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))+"\\");    }    Toast.makeText(this, buf.toString(), Toast.LENGTH_LONG).show(); break; case Menu.FIRST+2://刪除數據  super.getContentResolver().delete(Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_URI, contactsId),  null, null);//刪除指定數據    this.allContacts.remove(position);//刪除數據項    this.simpleadapter.notifyDataSetChanged();//更新列表項    Toast.makeText(this, "數據已經刪除", Toast.LENGTH_LONG).show(); break; } return super.onContextItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {//創建上下文菜單 super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("聯系人操作"); menu.add(Menu.NONE, Menu.FIRST+1, 1, "查看詳情"); menu.add(Menu.NONE, Menu.FIRST+2, 2, "刪除聯系人"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); this.contactslist=(ListView)super.findViewById(R.id.contactslist); this.result=super.getContentResolver().query( ContactsContract.Contacts.CONTENT_URI, null, null, null, null); super.startManagingCursor(result); for(result.moveToFirst();!result.isAfterLast();result.moveToNext()){ Map<String,Object> map=new HashMap<String, Object>(); map.put("_id",result.getInt(result.getColumnIndex( ContactsContract.Contacts._ID))); map.put("name", result.getString(result.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME))); this.allContacts.add(map); } this.simpleadapter=new SimpleAdapter( this, this.allContacts, R.layout.contacts, new String[]{"_id","name"}, new int[]{R.id._id,R.id.name}); this.contactslist.setAdapter(this.simpleadapter); this.registerForContextMenu(this.contactslist); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }   權限設置   <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.contactproject"     android:versionCode="1"     android:versionName="1.0" >         <uses-sdk         android:minSdkVersion="10"         android:targetSdkVersion="10" />         <application         android:allowBackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/AppTheme" >         <activity             android:name="com.example.contactproject.MainActivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                     <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application>     <uses-permission android:name="android.permission.READ_CONTACTS"/>      <uses-permission android:name="android.permission.WRITE_CONTACTS"/> </manifest>    
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved