public class MainActivity extends Activity { private ListView listview; private EditText info_phonenumber;// 輸入號碼 private TextView empty; private ArrayAdapter<String> adapter; private Map<String, Object> mapUser;// 存放號碼的map @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) this.findViewById(R.id.listview); info_phonenumber = (EditText) this.findViewById(R.id.info_text); empty = (TextView) this.findViewById(R.id.empty); // 給listview註冊上下文 listview.setEmptyView(empty);// 這一步設置當沒有數據時才顯示這個empty控件 mapUser = new HashMap<String, Object>(); List<String> list = getBlacklist(); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); listview.setAdapter(adapter); registerForContextMenu(listview);//給上下文菜單註冊 } @Override//上下文菜單 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { AdapterContextMenuInfo contextMenuInfo = (AdapterContextMenuInfo) menuInfo; menu.setHeaderIcon(R.drawable.ic_launcher); // 裝換成:AdapterContextMenuInfo後獲取選取的那一項的位置(position) menu.setHeaderTitle("提示:" + getBlacklist().get(contextMenuInfo.position)); getMenuInflater().inflate(R.menu.menu, menu); super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo contextMenuInfo = (AdapterContextMenuInfo) item .getMenuInfo(); // 拿到該位置的內容,即號碼 String key = getBlacklist().get(contextMenuInfo.position); switch (item.getItemId()) { case R.id.delete: mapUser.remove(key); fillListView(); break; case R.id.noaction: break; } return super.onContextItemSelected(item); } // 按鈕事件 public void checkButton(View view) { switch (view.getId()) { case R.id.bnt_add: String phonenumber = info_phonenumber.getText().toString(); // 將號碼添加到map中 mapUser.put(phonenumber, null); fillListView(); break; case R.id.bnt_clear: mapUser.clear(); fillListView(); break; } } // 更新數據 public void fillListView() { adapter.clear(); adapter.addAll(getBlacklist()); } private List<String> getBlacklist() { List<String> list = new ArrayList<String>(); for (Map.Entry<String, Object> entry : mapUser.entrySet()) { list.add(entry.getKey());// 將號碼放到集合中 } return list; } } //佈局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <EditText android:id="@+id/info_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入黑名單號碼" /> <LinearLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/info_text" android:orientation="horizontal" > <Button android:id="@+id/bnt_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="checkButton" android:text="添加黑名單" /> <Button android:id="@+id/bnt_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="checkButton" android:text="刪除黑名單" /> </LinearLayout> <TextView android:id="@+id/show_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/layout" android:text="如下是添加的數據" /> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/show_text" /> <TextView android:id="@+id/empty" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/listview" android:text="沒有更多內容" /> </RelativeLayout>