Menu (iv) Add a context menu to the ListView item

Source: Internet
Author: User

<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 "    android:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_ Horizontal_margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen /activity_vertical_margin "    tools:context=" com.example.contextmenu.MainActivity ">    <listview        Android:id= "@+id/listview1"        android:layout_width= "match_parent"        android:layout_height= "Wrap_content"        android:layout_alignparentleft= "true"        android:layout_alignparenttop= "true" >    </listview ></RelativeLayout>
Package Com.example.contextmenu;import Java.util.arraylist;import Java.util.list;import android.app.Activity; Import Android.os.bundle;import android.view.contextmenu;import Android.view.menu;import android.view.MenuInflater ; Import Android.view.menuitem;import Android.view.view;import Android.view.contextmenu.contextmenuinfo;import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.toast;public class MainActivity Extends Activity {private ListView listview; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); listview = (ListView) Findviewbyid ( R.ID.LISTVIEW1); arrayadapter<string> arrayadapter = new Arrayadapter<string> (Mainactivity.this, Android. R.layout.simple_list_item_1,getdata ()); Listview.setadapter (Arrayadapter); Registerforcontextmenu (ListView);// Register the context menu for the ListView}public list<string> getData () {list<string> List = new arraylist<string> (); for (int i = 1; I < 10; i++) {list.add ("list" + i);} return list; } @Overridepublic void Oncreatecontextmenu (ContextMenu menu, View v,contextmenuinfo menuinfo) {// Load the context menu in XML super.oncreatecontextmenu (menu, V, menuinfo); Menuinflater menuinflater = Getmenuinflater (); Menuinflater.inflate (R.menu.main, menu);} @Overridepublic boolean oncontextitemselected (MenuItem item) {switch (Item.getitemid ()) {case R.id.edit: Toast.maketext (Mainactivity.this, "edit operation", Toast.length_short). Show (); Break;case R.id.share:toast.maketext ( Mainactivity.this, "Share operation", Toast.length_short). Show (); Break;case R.id.delete:toast.maketext (Mainactivity.this, " Delete operation ", Toast.length_short). Show (); break;default:break;} return super.oncontextitemselected (item);}}

Res/menu/main.xml

<menu xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http://schemas.android.com /tools "    tools:context=" com.example.contextmenu.MainActivity ">    <item        android:id=" @+id/edit "        android:orderincategory= "android:showasaction="        never "        android:title=" edit/>    <item        android:id= "@+id/share"        android:orderincategory= "        android:showasaction=" never "        android: title= "Share"/>    <item        android:id= "@+id/delete"        android:orderincategory= "Android"        : Showasaction= "Never"        android:title= "Delete"/></menu>

Menu (iv) Add a context menu to the ListView item

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.