This post will show how to implement Context menu for Custom Cursor Adapter.

Steps:

1. Create xml value and place it in your strings.xml file

<string-array name=”menu”>
<item>Edit</item>
<item>Delete</item>
<item>Share</item>
</string-array>

2. Create item list layout for your listview to be shown, name as (example): listitem.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”><TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:id=”@+id/displayname”
android:textSize=”18dip”
android:textStyle=”bold”
android:singleLine=”true”
android:ellipsize=”end”/><LinearLayout
android:orientation=”horizontal”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”><TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:id=”@+id/title”
android:textSize=”15dip”
android:singleLine=”true”
android:ellipsize=”end”
android:layout_weight=”1.0″/><TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/duration”
android:gravity=”right”
android:textSize=”15dip”
android:singleLine=”true”
android:ellipsize=”end”/>
</LinearLayout></LinearLayout>

3. create your cursor adapter in onCreate() method at your main activity

String selection = MediaStore.Audio.Media.DATA + ” like ?”;Cursor cursor =  managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, STAR ,  selection, new String[]  {“%”+SoundRecorderPreferenceActivity.getSavePath(this)+”%”}, null);if(null != cursor)
{
cursor.moveToFirst();mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem, cursor);setListAdapter(mediaAdapter);registerForContextMenu(getListView()); // call this method after setListAdapter;
}

4. Create a sub class for Custom Media Adapter that extends Simple  Cursor Adapter. This class is created to handle a list in media file,  such as mp3 file, and display its attribute to our listView

private class MediaCursorAdapter extends SimpleCursorAdapter{public MediaCursorAdapter(Context context, int layout, Cursor c) {
super(context, layout, c,
new String[] { MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION},
new int[] { R.id.displayname, R.id.title, R.id.duration });
}@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView)view.findViewById(R.id.title);
TextView name = (TextView)view.findViewById(R.id.displayname);
TextView duration = (TextView)view.findViewById(R.id.duration);name.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));title.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));int durationInMs = Integer.parseInt(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));int durationInSecs = durationInMs/1000;
int durationInMin = (durationInSecs)/60;durationInSecs = durationInSecs %60;String dur = String.format(“%02d:%02d”, durationInMin,durationInSecs);duration.setText(“” + dur);view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.listitem, parent, false);bindView(v, context, cursor);return v;
}
}

5. Create a method when an item is pressed long

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
String[] menuItems = getResources().getStringArray(R.array.menu);  //we’ll show context menu, already define in your strings.xml above
for (int i = 0; i<menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}

6. Create a method when a menu is selected. (in example):

@Override  public boolean onContextItemSelected(MenuItem item) {    switch (item.getItemId()) {      case DELETE_ID:        AdapterView.AdapterContextMenuInfo info=          (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();        delete(info.id);        return(true);    }    return(super.onOptionsItemSelected(item));  }

Thats all…

hope it helps..

Some references:http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html

http://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/