本文主要介紹採用ListView與上下文菜單的配合使用,其中主要是:經過ContentProvider獲取手機中聯繫人列表而後用ListView的形式顯示出來,經過ContextMenu操做ListView中的內容。其主要代碼的實現以下: java
- package org.lxh.demo;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.ContactsContract;
- 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 ContactsDemo extends Activity {
- /** 返回結果的遊標 */
- private Cursor result = null;
- /** 顯示信息 */
- private ListView contactsList = null;
- /** 存儲數據信息 */
- private List<Map<String, Object>> allContacts = null;
- /** 數據適配器 */
- private SimpleAdapter simple = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- super.setContentView(R.layout.main);
- // 實例化ListView
- this.contactsList = (ListView) super.findViewById(R.id.contactsList);
- // 實例化遊標
- this.result = super.getContentResolver().query(
- ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
- // 將結果集交給容器管理
- super.startManagingCursor(this.result);
- // 實例化List集合
- this.allContacts = new ArrayList<Map<String, Object>>();
- // 取出結果集中的每個內容
- for (this.result.moveToFirst(); !this.result.isAfterLast(); this.result
- .moveToNext()) {
- // 實例化HashMap
- Map<String, Object> contact = new HashMap<String, Object>();
- // 存儲一條記錄
- contact.put("_id", this.result.getInt(this.result
- .getColumnIndex(ContactsContract.Contacts._ID)));
- // 存儲一條記錄
- contact.put("name", this.result.getString(this.result
- .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
- // 向List中添加一條記錄
- this.allContacts.add(contact);
- }
- // 實例化數據適配器
- this.simple = new SimpleAdapter(this, this.allContacts,
- R.layout.contacts, new String[] { "_id", "name" }, new int[] {
- R.id._id, R.id.name });
- this.simple
- .setDropDownViewResource(android.R.layout.simple_list_item_checked);
- // 爲ListView設置適配器
- this.contactsList.setAdapter(this.simple);
- // 給ListView註冊上下文菜單
- super.registerForContextMenu(this.contactsList);
- }
- /** 上下文菜單被選中時觸發的事件 */
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- // 實例化info進而經過info.position獲得ListView中的那一項(id)被選中,從而生產上下文菜單並顯示
- AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
- .getMenuInfo();
- int position = info.position;
- // 獲取聯繫人的id
- String contactsId = this.allContacts.get(position).get("_id")
- .toString();
- // 進行上下文菜單的操做
- switch (item.getItemId()) {
- // 查看
- case Menu.FIRST + 1:
- // 查詢的條件
- String phoneSelection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID
- + "=?";
- // 查詢條件所知足的值
- String[] phoneSelectionArgs = new String[] { contactsId };
- // 得到查詢結果集
- Cursor c = super.getContentResolver().query(
- ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
- phoneSelection, phoneSelectionArgs, null);
- // 實例化StringBuffer
- StringBuffer buf = new StringBuffer();
- buf.append("電話號碼是:");
- // 遍歷查詢的結果集
- for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
- buf.append(
- c.getString(c
- .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)))
- .append("、");
- }
- // 顯示查詢的結果
- Toast.makeText(this, buf, Toast.LENGTH_SHORT).show();
- break;
- // 刪除
- case Menu.FIRST + 2:
- //將所選定的項刪除
- super.getContentResolver().delete(
- Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,
- contactsId), null, null);
- // 刪除集合數據項
- this.allContacts.remove(position);
- // 通知adapter改變
- this.simple.notifyDataSetChanged();
- //顯示結果
- Toast.makeText(this, "數據已刪除!", Toast.LENGTH_SHORT).show();
- break;
- }
- return super.onContextItemSelected(item);
- }
- /** 長按某一條目時生成上下文菜單觸發的事件 */
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenuInfo menuInfo) { // 建立菜單
- //此處的View 爲 ListView 此外咱們能夠經過 AdapterView.AdapterContextMenuInfo info =
- (AdapterView.AdapterContextMenuInfo) menuInfo;
- // 而後info.position來獲取產生上下菜單的ListView中某個條目的id
- super.onCreateContextMenu(menu, v, menuInfo);
- // 設置上下文菜單的標題
- menu.setHeaderTitle("聯繫人操做");
- // 設置上下文菜單的選項"查看詳情"
- menu.add(Menu.NONE, Menu.FIRST + 1, 1, "查看詳情");
- // 設置上下文菜單的選項"刪除信息"
- menu.add(Menu.NONE, Menu.FIRST + 2, 1, "刪除信息");
- }
- }
其效果以下: android
採用ListView顯示聯繫人列表: app
當長按ListView中的某一項時彈出上下文菜單: ide
當點擊查看詳情時彈出一個Toast this