Question

I am new in Android developing, and my question is about what is the best way to provide options for listview items? On the Bada OS, you can slide any listview item horizontally away, and access the options for that item. http://horrorcoding.altervista.org/developing-on-bada-2-0-%E2%80%93-how-to-use-listcontextitems-in-a-listviewgroupedlistview/

But this does not exist on Android. So how to do it? My main activity has a listview, if you click on an item, it opens a new activity with the details of the clicked item.

But on the main activity, you should be able to delete or edit any item. So my ideas:

idea1: I put a small edit icon on the top-right corner of the item, and when user click on this, I display QuickAction with Edit/Delete options. http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/ If user clicks on other part of the item, just open the details activity.

idea2: I use contextmenu for long-press on the item. But this option is "invisible" for the users, so how can they know that they can do a long-press on the items?

What do you think?

Was it helpful?

Solution

The usual method is to create a context menu for the list (activated by a long click on a list item).

And the "long press for context menu" is an Android standard. Any user is aware of it, no need to provide an indicator (IMO). Since you are making an Android app, I would stick with the Android convention. :)

It sounds like you know how to implement it, but in case you dont...

First, you register your list for a context menu:

...
setListAdapter(lists);
registerForContextMenu(getListView());

Then you provide code ot create the context menu (you can create it from an XML resource too, but I don't have an example of that handy):

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Item Operations");
    menu.add(0, v.getId(), 0, "Edit Item");
    menu.add(0, v.getId(), 0, "Delete Item");
}

Then you provide the code to handle the options in the menu:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();
    if (item.getTitle() == "Edit Item") {
        mRowId = info.id;
        DialogFragment_Item idFragment = new DialogFragment_Item();
        idFragment.show(getFragmentManager(), "dialog");
    } else if (item.getTitle() == "Delete Item") {
        mDbHelper.deleteItem(info.id);
        return true;
    }
    return super.onContextItemSelected(item);
}

OTHER TIPS

The long-press on the item is the most common way to implements your options, so users are aware about this kind of thing. I think this is the way to follow. However you can also implement your button, or a slide action wich shows the same options as your long-press (official twitter app for example).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top