I have a ListView which creates a ContextMenu on long press of one of its elements. How do I find the element that was selected in the ListView that created this context menu (not the selected MenuItem)? Here is my code:
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, final View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Actions");
android.view.MenuItem remove = menu.add("Remove");
final int selectedItem = ((ListView)v).getSelectedItemPosition();
remove.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(android.view.MenuItem item) {
doSomething(listAdapter.getItem(selectedItem)); // NPE here
return true;
}
});
}
});
Please note that I do not want the item that was selected from the context menu, but the ListView item that triggered this context menu instead.
Related
I need to change list of menu items(actually hide one or just make it not clickable) depending on list item long-clicks.
I asume I can do it somehow in setOnItemLongClickListener but can't find solution:
public void onCreate(Bundle savedInstanceState){
......
registerForContextMenu(listView);
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView , View v,int position, long id) {
MyListItem item = (MyListItem) listView.getItemAtPosition(position);
listView.showContextMenu();
return true;
}
});
Just found only solution with global variable to set value in OnItemLongClickListener depend on item clicked values and check it in onCreateContextMenu. Also can save there just clicked item (MyListItem). But I think exists more elegant solution.
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if(!someGlobalVar){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_item, menu);
}
...
or
if(!someGlobalVar){
menu.getItem(0).setVisible(someGlobalVar);
}
}
I have a LisView with several items. To this I've connected an OnItemClickListener (as an inner class), like this:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ShoppingListApp02Activity.this, "List item selected:" +
items.get(position).getId(), Toast.LENGTH_LONG).show();
}
});
As is obvious, selecting an entriy displays elements of the object of that entry, in this example the selected Item object's ID (not the list ID, but the objects ID, set when creating the ArrayList items). This works nicely, and enables me to do anything I want with the selected item(s).
Now I'd like to also have a "long-click" listener her, which opens a context menu for the selected ListView item. How do I do that? I've been able to attach an onCreateContextMenu listener to the ListView, but I don't see how I can get the elements of the ArrayList as with the onItemClickListener?
Here's what I've got:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(0, v.getId(), 0, "Something");
menu.add(0, v.getId(), 0, "Something else");
}
Since OnCreateConextMenu takes different parameters than the OnItemClickListener, how to I access the ArrayList's elements like in the OnItemClickListener?
If you decide you still want to use the context menu paradigm:
Consider this for working with lists:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// Get the list
ListView list = (ListView)v;
// Get the list item position
AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
int position = info.position
// Now you can do whatever.. (Example, load different menus for different items)
list.getItem(position);
...
}
Instead of messing with context menus (which are used in a wide context - like right-click in PC), ListView offers onItemLongClick event which is a lot more easier to implement. For example:
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
return false;
}
});
This will help you to achieve long-pressed actions on a row.
Open the context menu of the view within the event handler for the long press event on the row view.
convertView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
((Activity)mContext).openContextMenu(v);
return true;
}
});
This way both the click on the view and the long press context menu works on the listview row item.
First register context menu into your listview to open context menu:
registerForContextMenu(YOUR LIST-VIEW OBJECT);
then you can use your onCreateContextMenu() method:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(0, v.getId(), 0, "Something");
menu.add(0, v.getId(), 0, "Something else");
}
You actually dont need to use longClickListener with your listview to use ContextMenu.
Hope it will help you.
I will let you go through the below written example and see how it is implemented by using onContextItemSelected()
public void onCreateContextMenu(ContextMenu menu,
View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(title);
menu.add(0, CMD_EDIT, 0, R.string.context_menu_edit);
menu.add(0, CMD_DELETE, 0, R.string.context_menu_delete);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case CMD_EDIT:
any_function();//add your functionality here i.e. what you want to do
return true;
case CMD_DELETE:
**confirmDelete**();
return true;
default:
return super.onContextItemSelected(item);
}
}
Hope this helps...
Try this for a View item in recycleView
viewForContextMenu.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add("Context Menu Item1").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
//What should happent on selecting context menu item 1
return true;
}
});
}
});
You can use it with setting data to a ViewHolder item
I have a ListView. I implemented OnItemClickListener to open a ContextMenu when an item is clicked.
lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
showMenu(view);
}
});
And the code to create menu.
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.friend_list_menu, menu);
}
public void showMenu(View v) {
registerForContextMenu(v);
openContextMenu(v);
}
My problem is, when I clicked on one item of ListView, it does open the Menu. But if I go back to the ListView, I can't click that item again. The same for other items, it can't be click after close the menu. Can anyone help me with this?
You've set that up incorrectly. You register for context menu when you set the adapter, not in a button click.
It should look like this:
setListAdapter(lists);
registerForContextMenu(getListView());
Then you have your onCreateContextMenu and onContextItemSelected methods (I create mine programatically, but your inflated one woudl work just as well):
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("List Operations");
menu.add(0, v.getId(), 0, "Edit List");
menu.add(0, v.getId(), 0, "Delete List");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
if (item.getTitle() == "Edit List") {
// Do your edit stuff here
} else if (item.getTitle() == "Delete List") {
// Do your delete stuff here
}
return super.onContextItemSelected(item);
}
You don't need to use onItemClick unless you want to do something on a short press of the item (context menu is long press).
The following code will works.
code:
ListView listview=(ListView)findViewByid(R.id.listview);
/**** here write appending data to listview*******/
ArrayAdapter<String> adp=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,/**your list items**/);
listview.setadapter(adp);
registerForContextMenu(listview);
//listview item click listener
listview.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int p, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "selected" + p, 30).show();
}});
/****do your context menu code here*******/
I have a CustomListAdapter. I have overloaded my OnItemClickListener and added a registerForContextMenu line for the position that i have a context menu shown.
When i select the Item that should show the MenuItem, the menu is shown. When i press the back button on the phone the menu disappears. However now what happens that the same Item in my listview does not receive the OnItemClickListener anymore. Am i making sense ? I mean after the menu disappears, the same item does not receive the click listener. The items above and below receive the event as desired. I seems as if the Menu has disappeared but still is catching the click event ?
It's bad. You have to call registerForContextMenu in onCreate method.
So try it like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts);
registerForContextMenu(<yourListView>);
...
}
for create ContextMenu you have to override onCreateContextMenu method
#Override
public void onCreateContextMenu(ContextMenu cMenu, View parent, ContextMenu.ContextMenuInfo info) {
this.contextMenu = cMenu;
new MenuInflater(Contacts.this).inflate(R.menu.conmenu, this.contextMenu);
}
and for select items override onContextItemSelected method:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.c_odobrat:
deleteContactDialog(info.id);
return true;
}
return false;
}
And it should works.
quickLinkListView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Intent intent = new Intent();
intent.setClassName(Home.this, "com.myapp.toc" + Constants.ACT_NAMES[position]);
if (position < 4 && position > 1)
{
switch (position)
{
case 3:
registerForContextMenu(v);
ViewHolder.v=v;
openContextMenu(v);
break;
}
}
}
});
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_home, menu);
menu.setHeaderTitle("Select Video Type");
}
I have implemented a custom list view. (using Activity and ListRowAdapter extending BaseRowAdaptor)
I set a contextmenu to listview setting
itemView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener()
{
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Action 1");
// menu.add(0, v.getId(), 0, "Action 2");
}
});
In getView method in my adapter class.
But How t handle the item select event?
I can catch that event in My Activity.
But how can i identify the row of the list view?
You can create a global int variable that contains the index of the item that you have selected. You would set the value of the variable in the listview onLongClick(), onClick(), etc event. Then when you are inside the context menu's item click event, you will have access the currently selected row in the listview.
set LongClickListener to listview then showContext menu on longClick .
Class ActivityName extends Activity implements IconContextItemSelectedListener
{
private IconContextMenu cmContextMenu;
private int selectedPosition ;
onCreate() {
cmContextMenu = new IconContextMenu(this, R.menu.fbupdatecontextmenu);
cmContextMenu.setOnIconContextItemSelectedListener(this);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long arg3) {
selectedPosition = position;
cmContextMenu.show();
return true;
}
});
}
public void onIconContextItemSelected(MenuItem item, Object info) {
switch (item.getItemId()) {
case R.id.someThing: {
system.out.prinlt("position " +selectedPosition);
break;
}
}
}