I have an app that lists items in a listview which is populated from Parse.com and when I add new items to listview it adds to Parse.com
That is working fine but I also have a popup context menu window that appears when you longpress the item.
I can get it to remove the item from the list but not to delete from Parse.com
Can anyone please help me?
This is my code for the context window;
`#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Options");
getMenuInflater().inflate(R.menu.action, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
String name = Name.getText().toString();
switch (item.getItemId()) {
case R.id.cnt_mnu_x2:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
break;
case R.id.cnt_mnu_custom:
Toast.makeText(this, "TEST SELECTED", Toast.LENGTH_SHORT).show();
break;
case R.id.cnt_mnu_delete:
adapter.remove(adapter.getItem(info.position));
Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
break;
}
return true;
}`
Related
As far as I knew Context Menu can be registered for any view not only for ListView. Today I am trying to register context menu for an Custom ImageView. Like this:
// In onCreateView() method of activity
registerForContextMenu(mViewHolder.profileImageView);
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
switch (v.getId()) {
case R.id.profile_imageview:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.add(Menu.NONE, CONTEXT_MENU_ITEM_CHANGE_PICTURE, 0, "Change Picture");
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
switch (item.getItemId()) {
case CONTEXT_MENU_ITEM_CHANGE_PICTURE:
//Toast.makeText(mParentActivity, "Delete " + info.position + "th item", Toast.LENGTH_LONG).show()
Toast.makeText(this, "Go to library", Toast.LENGTH_SHORT).show();
return true;
default:
return true;
}
}
I didn't find any example of registering context menu without ListView. Would anyone give me one example or can figure out what I am missing ?
you need to write super.onCreateContextMenu(menu, v, menuInfo);
after all code like:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
switch (v.getId()) {
case R.id.profile_imageview:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.add(Menu.NONE, CONTEXT_MENU_ITEM_CHANGE_PICTURE, 0, "Change Picture");
super.onCreateContextMenu(menu, v, menuInfo);
}
}
I wanted to write an app which can store some messages. Currently by doing long press on android messages some options (like "forward message", "delete message" etc) are arrived. I would like to add one more option to this (for example "store this message").
Is there a way to do this?
Try this code:
ListView nameList;
nameList = (ListView) findViewById(R.id.list);
nameList.setLongClickable(true);
registerForContextMenu(nameList);
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle("Delete / Miss Call Contact");
menu.add(menu.NONE,1,menu.NONE,"Delete");
menu.add(menu.NONE,2,menu.NONE,"call");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
position = (int)info.id;
switch(item.getItemId()) {
case DELETE:
// do something
case MISSCALL:
// do something
}
return super.onContextItemSelected(item);
}
I have a list view connected to a database, showing a all the entries. I want a menu to show up if the user long clicks a item in the listview(database entry), showing options to edit or delete the entry. how can i do this.
Till now, I have tried using a onItemLongClick listener and a toast in it showing the id of the view long clicked.
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
String res = Long.toString(id);
Toast toast = Toast.makeText(this, res, Toast.LENGTH_SHORT);
toast.show();
return true;
}
First you need to register your context menu on list view.
lv = (ListView) findViewById(R.id.list_view);
registerForContextMenu(lv);
Then, just override activity methods.
/**
* MENU
*/
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.list_view) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.add:
// add stuff here
return true;
case R.id.edit:
// edit stuff here
return true;
case R.id.delete:
// remove stuff here
return true;
default:
return super.onContextItemSelected(item);
}
}
Here is an example of menu_list.xml file (you have to put the file in the res/menu folder).
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/add"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/menu_add" />
<item android:id="#+id/edit"
android:icon="#android:drawable/ic_menu_edit"
android:title="#string/menu_edit" />
<item android:id="#+id/delete"
android:icon="#android:drawable/my_icon_delete"
android:title="#string/menu_delete" />
</menu>
Hope it will help.
Instead of using onItemLongClick you can use
public void onCreateContextMenu(final ContextMenu menu,
final View v, final ContextMenuInfo menuInfo) {
...
}
where you setup the options for edit and delete or whatever you need to.
The actions for the item selected from the context menu can be processed in
public boolean onContextItemSelected(final MenuItem item)
For more information on context menu see here.
For a step by step tutorial visit here.
Edit
The second link is broken as it was quite old.
But I guess you can refer one of the other highly voted answer to see all the steps involved,
Another approach:
//Deleted individual cart items
//on list view cell long press
cartItemList.setOnItemLongClickListener (new OnItemLongClickListener() {
#SuppressWarnings("rawtypes")
public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) {
final CharSequence[] items = { "Delete" };
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Action:");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
cart = cartList.get(position);
db.removeProductFromCart(context, cart);
new AlertDialog.Builder(context)
.setTitle(getString(R.string.success))
.setMessage(getString(R.string.item_removed))
.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(CartDetailsActivity.this, HomeScreen.class);
startActivity(intent);
}
})
.show();
}
});
AlertDialog alert = builder.create();
alert.show();
//do your stuff here
return false;
}
});
You can call Activity.openOptionsMenu() in your click item method
check here
http://developer.android.com/reference/android/app/Activity.html#openOptionsMenu%28%29
**
after register your context menu on list view
**
override onCreateContextMenu Method like this
#Override
public void onCreateContextMenu(ContextMenu menu,View v, ContextMenu.ContextMenuInfo menuInfo){
if (v.getId() == R.id.listView){
AdapterView.AdapterContextMenuInfo info =(AdapterView.AdapterContextMenuInfo)menuInfo;
MenuItem mnu1=menu.add(0,0,0,"Delete");
MenuItem mnu2=menu.add(0,1,1,"Share");
}
}
then coding for each item that can be selected
#Override
public boolean onContextItemSelected(MenuItem menuItem){
AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo)menuItem.getMenuInfo();
switch (menuItem.getItemId()) {
case 0:
Toast.makeText(this, "Delete Selected", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(this, "Share Selected", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
Use registerForContextMenu(); to link context menu to any View successor.
To access to selected ListItem data, simple use the AdapterView.AdapterContextMenuInfo.
E.g.:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo menuinfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
long selectid = menuinfo.id; //_id from database in this case
int selectpos = menuinfo.position; //position in the adapter
switch (item.getItemId()) {
case 1:
doSomething(selectid);
break;
case 2:
doSomethingElse(selectpos);
}
return super.onContextItemSelected(item);
}
A quick note for those still struggling, there're two methods
registerForContextMenu(list);
unregisterForContextMenu(list);
Make sure you pick the first.
What do i need to change to make this display into alert dialog?
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteNote(info.id);
fillData();
return true;
}
return super.onContextItemSelected(item);
}
I'm a beginner in android so i still don't know how to implements some things. I want this to appear as alert dialog. Currently the user needs to longkeypress to activate the delete. However i want to prompt it as an alert dialog and have choices for the user to choose from if he wants to really delete it or not.
In your case DELETE_ID do this:
new AlertDialog.Builder(this).setTitle("Confirm Delete")
.setMessage("Do you want to delete this blank?")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteNote(info.id);
fillData();
}
})
.setNeutralButton("Cancel", null) // don't need to do anything but dismiss here
.create()
.show();
You need to put the delete logic in the OK click listener.
You only need to implement the following function. It will work.
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
Log.e(LOGTAG, "Tao menu");
if(v == expList)
{
super.onCreateContextMenu(menu, v, menuInfo);
//AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;
// We know that each row in the adapter is a Map
//HashMap map = (HashMap) simpleAdpt.getItem(aInfo.position);
menu.setHeaderTitle("Options");
menu.add(1, 1, 1, "Reprint");
menu.add(1, 2, 1, "Void");
menu.getItem(0).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{
return true;
}
});
menu.getItem(1).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{
return true;
}
});
}
}
I have a ListView and would like to remove a row item when the user long clicks on selects Remove from the context menu.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Selection Options");
menu.add(0, v.getId(), 0, "Remove Symbol");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Remove Symbol"){
Toast.makeText(this, "Remove clicked!", Toast.LENGTH_SHORT).show();
}
else {
return false;
}
return true;
}
How can I get a reference to the row number that was clicked, so I can remove that index from my array?
In your onContextItemSelected callback, you can use this code to get the id of the item.
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
removeItemFromListById(info.id);
}
Source:
Creating Menus | Android Developers