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;
}
}
}
Related
I want when long pressing an item of my list to give an option delete and delete the item if it pressed.
//onCreate()
alreadyAddedFood = (ListView) findViewById(R.id.alreadyAddedList);
registerForContextMenu(alreadyAddedFood);
//END of onCreate()
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_general, menu);
menu.setHeaderTitle("Select The Action");
}
#Override
public boolean onContextItemSelected(MenuItem item){
if(item.getItemId()==R.id.delete){
//How to delete?
Toast.makeText(getApplicationContext(),"delete"+item,Toast.LENGTH_LONG).show();
}else{
return false;
}
return true;
}
UPDATE
I also have this class which i implement the onlongClickListener and it works fine but without giving the user the option to press delete like the photo below
public void alreadyAdded(String searchedMessage) {
itemsAdded.add(searchedMessage);
final ArrayAdapter<String>addedAdapter= new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,itemsAdded);
alreadyAddedFood.setAdapter(addedAdapter);
alreadyAddedFood.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
// #Override
// public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// itemsAdded.remove(position);
// addedAdapter.notifyDataSetChanged();
// Toast.makeText(AddFood.this, "Item Deleted", Toast.LENGTH_LONG).show();
// return true;
// }
// });
}
Add this in your onContextItemSelected :
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); // init the info to get the position from
items.remove(info.position); // remove the item from the list
adapter.notifyDataSetChanged(); //updating the adapter
You can do Something like this:
private ListView ls;
ls.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int arg2, long arg3) {
ls.remove(arg2);//where arg2 is position of item you click
myAdapter.notifyDataSetChanged();
return false;
}
});
You can pop up a dialog and ask the user for his confirmation.
However, it is less aesthetic. I would recommend one of these:
After the long click, show a delete option (using the delete icon rather than text) in the app-bar.
Do not support long click, instead use an swipe-able rows with a delete option (again, with an icon) in the right/left of each row. (I will recommend this library.
(gif was taken from here)
Normally for ListViews, I would do this when I wanted to get the position that the user clicked on a Context Menu.
public boolean onContextItemSelected(android.view.MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
position = info.position;
However, ever since I switched to a RecycleView, I now get a null pointer here.
The code above is in my main Activity (Fragment) while onCreateContextMenu() is done in the adapter as per the new way.
ItemView.setOnCreateContextMenuListener(this); is also done in the adapter (specifically the constructor).
There're 3 options:
You can pass getAdapterPosition() instead of MenuItem's order
private class ChipViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {
public ChipViewHolder(View itemView) {
super(itemView);
itemView.setOnCreateContextMenuListener(this);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Select The Action");
menu.add(0, ACTION_1_ID, getAdapterPosition(), "action 1");
menu.add(0, ACTION_2_ID, getAdapterPosition(), "action 2");
}
}
And then, in Activity listen to onContextItemSelected() and retrieve position by getOrder()
#Override
public boolean onContextItemSelected(MenuItem item) {
int clickedItemPosition = item.getOrder();
// do something!
return super.onContextItemSelected(item);
}
Use custom implementations of RecyclerView like Teovald/ContextMenuRecyclerView one
Setting MenuItem's clickListener (see https://stackoverflow.com/a/33179957/1658267 ) and handles it there.
Yes, it's very inconvenient API. You can choose the one you like most.
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 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.
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*******/