ListView與上下文菜單

原創做品,容許轉載,轉載時請務必以超連接形式標明文章 原始出處 、做者信息和本聲明。不然將追究法律責任。 http://splend.blog.51cto.com/3717743/965219

本文主要介紹採用ListView與上下文菜單的配合使用,其中主要是:經過ContentProvider獲取手機中聯繫人列表而後用ListView的形式顯示出來,經過ContextMenu操做ListView中的內容。其主要代碼的實現以下: java

  
  1. package org.lxh.demo;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import android.app.Activity;
  7. import android.database.Cursor;
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.provider.ContactsContract;
  11. import android.view.ContextMenu;
  12. import android.view.ContextMenu.ContextMenuInfo;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15. import android.view.View;
  16. import android.widget.AdapterView;
  17. import android.widget.ListView;
  18. import android.widget.SimpleAdapter;
  19. import android.widget.Toast;
  20. public class ContactsDemo extends Activity {
  21. /** 返回結果的遊標 */
  22. private Cursor result = null;
  23. /** 顯示信息 */
  24. private ListView contactsList = null;
  25. /** 存儲數據信息 */
  26. private List<Map<String, Object>> allContacts = null;
  27. /** 數據適配器 */
  28. private SimpleAdapter simple = null;
  29. @Override
  30. public void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. super.setContentView(R.layout.main);
  33. // 實例化ListView
  34. this.contactsList = (ListView) super.findViewById(R.id.contactsList);
  35. // 實例化遊標
  36. this.result = super.getContentResolver().query(
  37. ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
  38. // 將結果集交給容器管理
  39. super.startManagingCursor(this.result);
  40. // 實例化List集合
  41. this.allContacts = new ArrayList<Map<String, Object>>();
  42. // 取出結果集中的每個內容
  43. for (this.result.moveToFirst(); !this.result.isAfterLast(); this.result
  44. .moveToNext()) {
  45. // 實例化HashMap
  46. Map<String, Object> contact = new HashMap<String, Object>();
  47. // 存儲一條記錄
  48. contact.put("_id", this.result.getInt(this.result
  49. .getColumnIndex(ContactsContract.Contacts._ID)));
  50. // 存儲一條記錄
  51. contact.put("name", this.result.getString(this.result
  52. .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
  53. // 向List中添加一條記錄
  54. this.allContacts.add(contact);
  55. }
  56. // 實例化數據適配器
  57. this.simple = new SimpleAdapter(this, this.allContacts,
  58. R.layout.contacts, new String[] { "_id", "name" }, new int[] {
  59. R.id._id, R.id.name });
  60. this.simple
  61. .setDropDownViewResource(android.R.layout.simple_list_item_checked);
  62. // 爲ListView設置適配器
  63. this.contactsList.setAdapter(this.simple);
  64. // 給ListView註冊上下文菜單
  65. super.registerForContextMenu(this.contactsList);
  66. }
  67. /** 上下文菜單被選中時觸發的事件 */
  68. @Override
  69. public boolean onContextItemSelected(MenuItem item) {
  70. // 實例化info進而經過info.position獲得ListView中的那一項(id)被選中,從而生產上下文菜單並顯示
  71. AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
  72. .getMenuInfo();
  73. int position = info.position;
  74. // 獲取聯繫人的id
  75. String contactsId = this.allContacts.get(position).get("_id")
  76. .toString();
  77. // 進行上下文菜單的操做
  78. switch (item.getItemId()) {
  79. // 查看
  80. case Menu.FIRST + 1:
  81. // 查詢的條件
  82. String phoneSelection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID
  83. + "=?";
  84. // 查詢條件所知足的值
  85. String[] phoneSelectionArgs = new String[] { contactsId };
  86. // 得到查詢結果集
  87. Cursor c = super.getContentResolver().query(
  88. ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
  89. phoneSelection, phoneSelectionArgs, null);
  90. // 實例化StringBuffer
  91. StringBuffer buf = new StringBuffer();
  92. buf.append("電話號碼是:");
  93. // 遍歷查詢的結果集
  94. for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
  95. buf.append(
  96. c.getString(c
  97. .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)))
  98. .append("、");
  99. }
  100. // 顯示查詢的結果
  101. Toast.makeText(this, buf, Toast.LENGTH_SHORT).show();
  102. break;
  103. // 刪除
  104. case Menu.FIRST + 2:
  105. //將所選定的項刪除
  106. super.getContentResolver().delete(
  107. Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,
  108. contactsId), null, null);
  109. // 刪除集合數據項
  110. this.allContacts.remove(position);
  111. // 通知adapter改變
  112. this.simple.notifyDataSetChanged();
  113. //顯示結果
  114. Toast.makeText(this, "數據已刪除!", Toast.LENGTH_SHORT).show();
  115. break;
  116. }
  117. return super.onContextItemSelected(item);
  118. }
  119. /** 長按某一條目時生成上下文菜單觸發的事件 */
  120. @Override
  121. public void onCreateContextMenu(ContextMenu menu, View v,
  122. ContextMenuInfo menuInfo) { // 建立菜單
  123. //此處的View 爲 ListView 此外咱們能夠經過 AdapterView.AdapterContextMenuInfo info =
  124. (AdapterView.AdapterContextMenuInfo) menuInfo;
  125. // 而後info.position來獲取產生上下菜單的ListView中某個條目的id
  126. super.onCreateContextMenu(menu, v, menuInfo);
  127. // 設置上下文菜單的標題
  128. menu.setHeaderTitle("聯繫人操做");
  129. // 設置上下文菜單的選項"查看詳情"
  130. menu.add(Menu.NONE, Menu.FIRST + 1, 1, "查看詳情");
  131. // 設置上下文菜單的選項"刪除信息"
  132. menu.add(Menu.NONE, Menu.FIRST + 2, 1, "刪除信息");
  133. }
  134. }

其效果以下: android

採用ListView顯示聯繫人列表: app

當長按ListView中的某一項時彈出上下文菜單: ide

當點擊查看詳情時彈出一個Toast this

相關文章
相關標籤/搜索