导航

在ListView上创建一个ContextMenu

Posted on 2010-05-30 19:02  Enrico Zhang  阅读(185)  评论(0)    收藏  举报

 

layout中的main.xml

XML/HTML代码
  1. <?xml version ="1.0" encoding ="utf-8" ?>    
  2. <LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"    
  3.      android:orientation ="vertical"    
  4.      android:layout_width ="fill_parent"    
  5.      android:layout_height ="fill_parent" >    
  6.      <TextView    
  7.           android:layout_width ="fill_parent"    
  8.           android:layout_height ="wrap_content"    
  9.           android:text ="Long-Press on of the Items in the list." />    
  10.      <ListView android:id ="@+id/list_favorites"    
  11.           android:layout_width ="fill_parent"    
  12.           android:layout_height ="fill_parent" />    
  13. </LinearLayout>  

源代码是:

Java代码
  1. package com.gggeye.study;     
  2.      
  3. import java.util.ArrayList;     
  4.      
  5. import android.app.Activity;     
  6. import android.os.Bundle;     
  7. import android.view.ContextMenu;     
  8. import android.view.MenuItem;     
  9. import android.view.View;     
  10. import android.view.ContextMenu.ContextMenuInfo;     
  11. import android.view.View.OnCreateContextMenuListener;     
  12. import android.widget.ArrayAdapter;     
  13. import android.widget.ListView;     
  14.       
  15.      
  16. public class ContextMenuSample extends Activity {     
  17.      
  18.      // ===========================================================     
  19.      // Final Fields     
  20.      // ===========================================================     
  21.      protected static final int CONTEXTMENU_DELETEITEM = 0;     
  22.      
  23.      // ===========================================================     
  24.      // Fields     
  25.      // ===========================================================     
  26.      
  27.      protected ListView mFavList;     
  28.      protected ArrayList<Favorite> fakeFavs = new ArrayList<Favorite>();     
  29.      
  30.      // ===========================================================     
  31.      // "Constructors"     
  32.      // ===========================================================     
  33.      
  34.      /** Called when the activity is first created. */     
  35.      @Override     
  36.      public void onCreate(Bundle icicle) {     
  37.           super.onCreate(icicle);     
  38.           setContentView(R.layout.main);     
  39.      
  40.           /* Add some items to the list the listview will be showing. */     
  41.           fakeFavs.add(new Favorite("John""nice guy"));     
  42.           fakeFavs.add(new Favorite("Yasmin""hot girl"));     
  43.           fakeFavs.add(new Favorite("Jack""cool guy"));     
  44.      
  45.           this.mFavList = (ListView) this.findViewById(R.id.list_favorites);     
  46.           initListView();     
  47.      }     
  48.      
  49.      private void refreshFavListItems() {     
  50.           mFavList.setAdapter(new ArrayAdapter<Favorite>(this,     
  51.                     android.R.layout.simple_list_item_1, fakeFavs));     
  52.      }     
  53.      
  54.      private void initListView() {     
  55.           /* Loads the items to the ListView. */     
  56.           refreshFavListItems();     
  57.      
  58.           /* Add Context-Menu listener to the ListView. */     
  59.           mFavList.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {     
  60.      
  61.                public void onCreateContextMenu(ContextMenu conMenu, View view , ContextMenuInfo info) {     
  62.                     conMenu.setHeaderTitle("ContextMenu");     
  63.                     conMenu.add(000"Delete this favorite!");     
  64.                        
  65.                     /* Add as many context-menu-options as you want to. */     
  66.                }     
  67.           });     
  68.      }     
  69.      
  70.      // ===========================================================     
  71.      // Methods from SuperClass/Interfaces     
  72.      // ===========================================================     
  73.      
  74.      @Override     
  75.      public boolean onContextItemSelected(MenuItem aItem) {     
  76.           ContextMenuInfo menuInfo = (ContextMenuInfo) aItem.getMenuInfo();     
  77.      
  78.           /* Switch on the ID of the item, to get what the user selected. */     
  79.           switch (aItem.getItemId()) {     
  80.                case CONTEXTMENU_DELETEITEM:     
  81.                     /* Get the selected item out of the Adapter by its position. */     
  82.                     Favorite favContexted = (Favorite) mFavList.getAdapter()     
  83.                          .getItem(0);     
  84.                     /* Remove it from the list.*/     
  85.                     fakeFavs.remove(favContexted);     
  86.      
  87.                     refreshFavListItems();     
  88.                     return true/* true means: "we handled the event". */     
  89.           }     
  90.           return false;     
  91.      }     
  92.      
  93.      // ===========================================================     
  94.      // Inner and Anonymous Classes     
  95.      // ===========================================================     
  96.      
  97.      /** Small class holding some basic */     
  98.      protected class Favorite {     
  99.      
  100.           protected String name;     
  101.           protected String kindness;     
  102.      
  103.           protected Favorite(String name, String kindness) {     
  104.                this.name = name;     
  105.                this.kindness = kindness;     
  106.           }     
  107.      
  108.           /** The ListView is going to display the toString() return-value! */     
  109.           public String toString() {     
  110.                return name + " (" + kindness + ")";     
  111.           }     
  112.      
  113.           public boolean equals(Object o) {     
  114.                return o instanceof Favorite && ((Favorite) o).name.compareTo(name) == 0;     
  115.           }     
  116.      }