I have a list view filled with data. I set up a context menu for the listview using the following code:
list.setOnCreateContextMenuListener
(
new View.OnCreateContextMenuListener()
{
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo)
{
AdapterContextMenuInfo mi =(AdapterContextMenuInfo) menuInfo;
menu.add(0, 0, 0, "Delete item");
}
}
);
I have the following method override to control de contextmenu menuitem selected:
#Override
public boolean onContextItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case 0:
ShowAlert("hello from delete item");
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
In this overridden method, how could I find the item of the list view that was clicked?
You can use the ContextMenu.ContextMenuInfo.
Something like that:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;
}
You can also get the exact View for which the menu is being displayed:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;
View view = info.targetView;
}
private static final int EDIT_ID = Menu.FIRST + 3;
private static final int DELETE_ID = Menu.FIRST + 4;
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, EDIT_ID, Menu.NONE, "Edit").setAlphabeticShortcut(
'e');
menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete")
.setAlphabeticShortcut('d');
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case EDIT_ID:
edit(info.id);
return (true);
case DELETE_ID:
delete(info.id);
return (true);
}
return (super.onOptionsItemSelected(item));
}
OK, to solve problem info null ****
use static member and set value from Position of your holder
to save the value in longclick method member for example:-
public class CurrentPosition {
public static int Pos{ get; set; }
}
public bool OnLongClick(View v)
{
CurrentPosition.Pos = Position;
return false;
}
and use in your context select item:
public override bool OnContextItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case 0:
return true;
case 1:
Toast.MakeText(this,CurrentPosition.Pos.ToString(), ToastLength.Long).Show();
return true;
case 2:
Toast.MakeText(this, "Save", ToastLength.Long).Show();
return true;
}
return true;
}
}
C# code
Related
How to delete item from ListView using onCreateContextMenu? There is some problem in the code below. Any solution?
When item in listview is long pressed a menu popups in which there is an option of delete.
public class ContextMenuTest extends AppCompatActivity {
ListView listView;
String []name={"ANKUSH", "ANOUSHKA", "SHIVA", "SOMANSHU"};
String current;
ArrayAdapter<String> adapter;
int pos;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_context_menu_test);
listView = (ListView) findViewById(R.id.lvContextMenu);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name);
listView.setAdapter(adapter);
registerForContextMenu(listView);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("SELECT OPTION");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_test, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId())
{
case R.id.delete:
adapter.remove(adapter.getItem(info.position));
break;
}
return true;
}
}
In the onContextItemSelected method, add adapter.notifyDataSetChanged() to notify the adapter of changes to the data.
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId())
{
case R.id.delete:
adapter.remove(adapter.getItem(info.position));
adapter.notifyDataSetChanged();
break;
}
return true;
}
This Works
public class ContextMenuTest extends AppCompatActivity {
ListView listView;
List<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_context_menu_test);
listView = (ListView) findViewById(R.id.lvContextMenu);
list.add("ANKUSH");
list.add("ANOUSHKA");
list.add("SHIVA");
list.add("SOMANSHU");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
registerForContextMenu(listView);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("SELECT OPTION");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_test, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId())
{
case R.id.delete:
list.remove(info.position);
adapter.notifyDataSetChanged();
return true;
default:
return super.onContextItemSelected(item);
}
}
}
I have multiple ListView in one activity with ContextMenu for each one of ListView.
Actually it should work fine with all the 7 ListViews,but it work for only Seventh List
this is my code.
ArrayAdapter<Course> adapter;
for(int i=0;i<7;i++){
courses = db.findFiltered(String.format("day == %d ", i), "startTime ASC");
ListView lv1 = (ListView) findViewById (arrayList[i]);
registerForContextMenu(lv1);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
adapter = new CourseListAdapter(this, courses);
lv1.setAdapter(adapter);
}
#Override
public void onCreateContextMenu(android.view.ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.editItem:
//do something
return true;
case R.id.showItem:
//do something
return true;
case R.id.deleteItem:
db.removeCourse(adapter.getItem(info.position);
adapter.remove(adapter.getItem(info.position));
return true;
default:
return super.onContextItemSelected(item);
}
}
please help
Adapter instance is set to last iteration ListView object. This is quick code change. Try this:
private ArrayAdapter<Course> selectedListViewAdapter;
...
for(int i=0;i<7;i++){
courses = db.findFiltered(String.format("day == %d ", i), "startTime ASC");
ListView lv1 = (ListView) findViewById (arrayList[i]);
registerForContextMenu(lv1);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
ArrayAdapter<Course> adapter = new CourseListAdapter(this, courses);
lv1.setAdapter(adapter);
}
#Override
public void onCreateContextMenu(android.view.ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
try {
ListView selectedListView = (ListView)v;
selectedListViewAdapter = (ArrayAdapter<Course>)selectedListView.getAdapter();
} catch(ClassCastException e) {
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.editItem:
//do something
return true;
case R.id.showItem:
//do something
return true;
case R.id.deleteItem:
if(null != selectedListViewAdapter) {
db.removeCourse(selectedListViewAdapter.getItem(info.position);
selectedListViewAdapter.remove(selectedListViewAdapter.getItem(info.position));
}
return true;
default:
return super.onContextItemSelected(item);
}
}
I'm using Contextual Action Barwith ListView (CHOICE MODE SINGLE). Everything works but I don't know how to retrieve the selected item.
listViewData.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listViewData.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (mActionMode != null) {
return false;
}
mActionMode = ((ActionBarActivity) getActivity()).startSupportActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_edit:
mode.finish();
return true;
case R.id.menu_share:
mode.finish();
return true;
case R.id.menu_delete:
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
how to retrieve the selected item
you can use ActionMode.setTag() in onItemLongClick to pass selected item position to onActionItemClicked using ActionMode.getTag() as :
mActionMode = ....;
mActionMode.setTag(position); //<<< set selected row position
view.setSelected(true);
and set selected item in onActionItemClicked :
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
int item_postion=Integer.parseInt(mode.getTag().toString());
String str_item=listViewData.getAdapter().getItem(item_postion);
....
}
I have a dialog that shows a list of items, I need to be able to edit/delete items in this list so I put a context menu in so when the user long presses on an item it they can choose what they want to do (edit or delete the item).
The problem is that onContextItemSelected never gets called when an item in the context menu is selected.
I checked to see if maybe the activity that created the dialog fragment is getting the callback but that is not either so why is there it not getting called? Can you not do a context menu in a dialog?
public class TypesDialogList extends DialogFragment implements LoaderManager.LoaderCallbacks<Cursor>,OnItemClickListener,OnCreateContextMenuListener{
ListView lv;
SimpleCursorAdapter mAdapter;
private int EDIT_TYPE = 1;
private int DELETE_TYPE = 2;
OnEditType mType;
public Dialog onCreateDialog(Bundle state){
final View v = getActivity().getLayoutInflater().inflate(R.layout.layer_dialog_layout, null, false);
lv = (ListView)v.findViewById(R.id.listView1);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setOnItemClickListener(this);
lv.setOnCreateContextMenuListener(this);
return new AlertDialog.Builder(getActivity()).setView(v).setPositiveButton("Add Type", new OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
}).setTitle("Type's").create();
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
long id = info.id;
if(id > 3){
menu.setHeaderTitle("Type Menu");
menu.add(Menu.NONE, EDIT_TYPE, 1, "Edit");
menu.add(Menu.NONE, DELETE_TYPE, 2, "Delete");
}else{
Toast.makeText(getActivity(),"Cannot edit type",Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
AdapterView.AdapterContextMenuInfo oMenuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
long id = oMenuInfo.id;
if(item.getItemId() == EDIT_TYPE){
}else if(item.getItemId() == DELETE_TYPE){
}
return true;
}
}
For anybody still looking for a workaround, I just solved this problem by creating an anonymous OnMenuItemClickListener that delegates back to onContextItemSelected(MenuItem item) and setting it on all the items in my menu.
#Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
// Creation/inflate menu here
OnMenuItemClickListener listener = new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
onContextItemSelected(item);
return true;
}
};
for (int i = 0, n = menu.size(); i < n; i++)
menu.getItem(i).setOnMenuItemClickListener(listener);
}
I am using the following method to add a ContextMenu to a custom view i have built but i want to know how to react to the press of that contextmenu.
This is not an Activity so i cannot do this:
#override
public boolean onOptionsItemSelected(MenuItem item) {
Here is the code
private View.OnCreateContextMenuListener vC = new View.OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu arg0, View arg1,
ContextMenuInfo arg2) {
// TODO Auto-generated method stub
arg0.add(0, 0, 0, "Call");
arg0.add(0, 1, 0, "Map");
arg0.add(0, 2, 0, "Market");
}
};
Update:
Here is a very simplified verion of my class.
public final class NewView extends View {
public NewView(Context context, AttributeSet attrs) {
super(context, attrs);
cntxt = context;
this.setLongClickable(true);
this.setOnLongClickListener(vLong);
this.setOnCreateContextMenuListener(vC);
}
private View.OnLongClickListener vLong = new View.OnLongClickListener() {
public boolean onLongClick(View view) {
showContextMenu();
return true;
}
};
private View.OnCreateContextMenuListener vC = new View.OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu arg0, View arg1,
ContextMenuInfo arg2) {
// TODO Auto-generated method stub
arg0.add(0, 0, 0, "Call");
arg0.add(0, 1, 0, "Map");
arg0.add(0, 2, 0, "Market");
}
};
}
Noone seem to answer exhaustively TS' question (and mine eventually), thus let me post a solution to this. Given the code above you can attach custom MenuItem's click handlers:
private View.OnCreateContextMenuListener vC = new View.OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0, 0, 0, "Call")
.setOnMenuItemClickListener(mMenuItemClickListener);
menu.add(0, 1, 0, "Map")
.setOnMenuItemClickListener(mMenuItemClickListener);
menu.add(0, 2, 0, "Market")
.setOnMenuItemClickListener(mMenuItemClickListener);
}
};
private OnMenuItemClickListener mMenuItemClickListener = new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case 0:
// do "Call"
return true;
case 1:
// do "Map"
return true;
case 2:
// do "Market"
return true;
}
return false;
}
};
};
Use item.getItemId() and create switch and cases based on the number returned by getItemId()
Something like this.
#override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case 1:
Log.i("FIRST ITEM: ", "CALL");
break;
case 2:
Log.i("2nd ITEM: ", "MAP");
break;
case 3:
Log.i("3rd ITEM: ", "Market");
break;
default:
}
}
I hope this is what you meant by reacting on menu items selection. :)
This does seem to be an odd inconsistency in Android - a View can create a context menu, but the handling of said menu can only happen in completely different code?
I'm also solving this with setOnMenuItemClickListener(). The documentation suggests this is reasonable, but it requires effort to set it up if you are using a MenuInflater.
public void onCreateContextMenu(final ContextMenu menu, final View v,
final ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
final MenuInflater menuInflater = this.menuInflater;
menuInflater.inflate(R.menu.context_date, menu);
final int length = menu.size();
for (int index = 0; index < length; index++) {
final MenuItem menuItem = menu.getItem(index);
menuItem.setOnMenuItemClickListener(this);
}
}
You could assign a variable to your View from your activity after you inflate the main layout (in `onCreate).
myView = (View) findViewById(R.id.my_view);
Next, do registerForContextMenu(myView);
Finally, you can override onCreateContextMenu where you can add what happens when the context menu appears.
In the view :
Let your view implements ContextMenuInfo
public class MyView extends View implements ContextMenuInfo
Then, override getContextMenuInfo to return your view.
#Override
protected ContextMenuInfo getContextMenuInfo() {
return this ;
}
In you activity :
Register your view for ContextMenu.
registerForContextMenu(myView);
Then access the view in your activity onContextItemSelected.
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case R.id.action_ctx_wordview_read_tts:
MyView myView = (MyView) item.getMenuInfo();
...
}
}
Note :
When I add an onClickListener to the MyView, the contextMenu stoped working. I had to add the following code to solve the problem.
tvWord.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});