layout中的main.xml

XML:
<?xml version ="1.0" encoding ="utf-8" ?>
<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:layout_width ="fill_parent"
          android:layout_height ="wrap_content"
          android:text ="Long-Press on of the Items in the list." />
     <ListView android:id ="@+id/list_favorites"
          android:layout_width ="fill_parent"
          android:layout_height ="fill_parent" />
</LinearLayout>

源代码是:

package com.gggeye.study;import java.util.ArrayList;import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;public class ContextMenuSample extends Activity {// ===========================================================// Final Fields// ===========================================================protected static final int CONTEXTMENU_DELETEITEM = 0;// ===========================================================// Fields// ===========================================================protected ListView mFavList;protected ArrayList<Favorite> fakeFavs = new ArrayList<Favorite>();// ===========================================================// "Constructors"// ===========================================================/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);setContentView(R.layout.main);/* Add some items to the list the listview will be showing. */fakeFavs.add(new Favorite("John", "nice guy"));fakeFavs.add(new Favorite("Yasmin", "hot girl"));fakeFavs.add(new Favorite("Jack", "cool guy"));this.mFavList = (ListView) this.findViewById(R.id.list_favorites);initListView();}private void refreshFavListItems() {mFavList.setAdapter(new ArrayAdapter<Favorite>(this,android.R.layout.simple_list_item_1, fakeFavs));}private void initListView() {/* Loads the items to the ListView. */refreshFavListItems();/* Add Context-Menu listener to the ListView. */mFavList.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {public void onCreateContextMenu(ContextMenu conMenu, View view , ContextMenuInfo info) {conMenu.setHeaderTitle("ContextMenu");conMenu.add(0, 0, 0, "Delete this favorite!");/* Add as many context-menu-options as you want to. */}});}// ===========================================================// Methods from SuperClass/Interfaces// ===========================================================@Overridepublic boolean onContextItemSelected(MenuItem aItem) {ContextMenuInfo menuInfo = (ContextMenuInfo) aItem.getMenuInfo();/* Switch on the ID of the item, to get what the user selected. */switch (aItem.getItemId()) {case CONTEXTMENU_DELETEITEM:/* Get the selected item out of the Adapter by its position. */Favorite favContexted = (Favorite) mFavList.getAdapter().getItem(0);/* Remove it from the list.*/fakeFavs.remove(favContexted);refreshFavListItems();return true; /* true means: "we handled the event". */}return false;}// ===========================================================// Inner and Anonymous Classes// ===========================================================/** Small class holding some basic */protected class Favorite {protected String name;protected String kindness;protected Favorite(String name, String kindness) {this.name = name;this.kindness = kindness;}/** The ListView is going to display the toString() return-value! */public String toString() {return name + " (" + kindness + ")";}public boolean equals(Object o) {return o instanceof Favorite && ((Favorite) o).name.compareTo(name) == 0;}}
}

Creating a ContextMenu on a ListView相关推荐

  1. Creating a ContextMenu in ListView

    layout中的main.xml XML: <?xml version ="1.0" encoding ="utf-8" ?>  <Linea ...

  2. Android攻城狮认识ContextMenu

    什么是ContextMenu? 例如Windows系统的右键菜单,在Android中长按某个文件夹,会跳出菜单,这个菜单就是ContextMenu. ContextMenu与OptionMenu的区别 ...

  3. android 动态contextmenu,Android成长日记-ContextMenu实现上下文菜单

    一. ContextMenu的组成 标题以及标题图标 菜单内容 菜单内容的点击事件 二. ContextMenu与OptionMenu的区别 OptionMenu对应的是activity,一个acti ...

  4. 摘:轻而易举拥有xp风格的界面

    轻而易举拥有xp风格的界面 Windows XP发布时,我们中许多人都为她华丽漂亮的界面折服和兴奋.然而,.NET1.0发布时,我们中又有许多人,包括我自己,对它不支持XP风格感到大失所望.可事实是, ...

  5. 轻而易举拥有xp风格的界面

        轻而易举拥有xp风格的界面 Windows XP发布时,我们中许多人都为她华丽漂亮的界面折服和兴奋.然而,.NET1.0发布时,我们中又有许多人,包括我自己,对它不支持XP风格感到大失所望.可 ...

  6. android fragment contextmenu,在 fragment 中,无法为listView项创建 contextMenu_android_开发99编程知识库...

    在 ListView 上單擊每個項目時創建菜單. 我創建了兩個場景:在 Activtity 和 Fragment 上創建 ContextMenu . 但是,它只在 Activity 上運行,而不是在 ...

  7. android 动态contextmenu,在Android中使用ContextMenu与ListView

    要从选定的ListView项中获取该项,请参考ContextMenuInfo对象(请参见下面的最后一个实现方法).完整解决方案如下: 1)在ListActivity类中为上下文菜单注册ListView ...

  8. android contextmenu listview,Android ListView ContextMenu

    问题 I'm trying to create a ContextMenu when user tap hold on an item in my ListView. However my code ...

  9. WPF ListView 数据绑定后,ListViewItem如何拥有ContextMenu

    这个需要重写ListView的Style. 首先在ListView的Style对应位置增加一个ContextMenu的声明: <!--ListViewItem的菜单绑定--><Con ...

最新文章

  1. 剑指Offer #12 数值的整数次方(快速幂)
  2. html 打开页面光标自动选中输入框_初识selenium之web自动化
  3. 前端学习(1736):前端系列javascript之创建webpack与loader使用
  4. 网站发布错误“遭遇战”
  5. 安卓案例:闪烁满天星
  6. python入门三:文件操作
  7. 数据集转换_为什么LSTM看起来那么复杂,以及如何避免时序数据处理差异和混乱...
  8. windows serve 2012部署操作系统之部署(三)
  9. Emacs支持外部程序的粘贴
  10. jdbc中excute,excuteUpdate,excuteQuery函数解释
  11. java字符串拼接还是格式化_Java中三种字符串的拼接(++ , String.format , StringBuilder.a...
  12. python代码怎么练_自己练习的Python代码(1)
  13. Ace Admin中表格按钮的使用方法——表格导出为xls、pdf,表格打印预览等
  14. VS2013各版本密钥
  15. 从IT时代到DT时代
  16. 计算机数字键盘如何用,电脑小数字键盘不能使用的原因和解决方法
  17. centos7搭建elastiflow
  18. Value of type java.lang.String cannot be conver...
  19. win10 资源管理器无限闪退
  20. 直播平台软件开发都使用了什么协议呢?

热门文章

  1. 基于Java+Swing+Socket实现中国象棋-网络版
  2. workstation安装Windows Server 2016
  3. Python爬虫+颜值打分,5000+图片找到你的Mrs. Right
  4. java log4j详解_log4j详解
  5. 帕累托法则/20:80法则/犹太法则
  6. 两对等位基因控制一对相对性状的规律(基因互作)
  7. saas系统项目总结
  8. win10 excel和ppt变成英文了,word正常,怎么解决?
  9. LeetCode(626):换座位 Exchange Seats(SQL)
  10. 【论文阅读】3D-CVF: Generating Joint Camera and LiDAR Features Using Cross-View Spatial Feature Fusion for