android操作通訊錄的連絡人

來源:互聯網
上載者:User

介面設定檔 <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>>();@Overridepublic boolean onContextItemSelected(MenuItem item) {//選中某個功能表項目AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();//取得功能表項目int position=info.position;//取得操作IDString contactsId=this.allContacts.get(position).get("_id").toString();//取得資料IDswitch(item.getItemId()){//判斷功能表項目IDcase 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);}@Overridepublic 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, "刪除連絡人");}@Overrideprotected 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);}@Overridepublic 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>  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.