Setting the activity content view directly to a ListView, the code
below works as expected. However, if you change the code (by
uncommenting the dialog lines, and commenting the setContentView
line), the context menu no longer works.
public class AndroidTest extends Activity {
protected static final int ENTRY_ADD = Menu.FIRST;
protected static final int ENTRY_REMOVE = Menu.FIRST + 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Dialog);
ListView lv = new ListView(this);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(
this, R.array.test,
android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);
lv.setCacheColorHint(0);
lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener
() {
public void onCreateContextMenu(ContextMenu menu, View
v,
ContextMenuInfo menuInfo) {
menu.add(0, ENTRY_ADD, 1, "Add");
menu.add(0, ENTRY_REMOVE , 2,"Remove");
}});
//Dialog d = new Dialog(this);
//d.setContentView(lv);
//d.show();
setContentView(lv);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
Log.d("Test", "Selected item " + info.position);
switch(item.getItemId()) {
case ENTRY_ADD:
Log.d("Test", "Add was clicked");
break;
case ENTRY_REMOVE:
Log.d("Test", "Remove was clicked");
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
}
My workaround may be using an activity composed of only a listview,
with the dialog theme - which should work fine. But that seems like
some additional work for something that should work as described above.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---