Reputation: 24998
My question is simple: How do I use long click to let the users select items from the ListView
? So far, I only know how to detect 'short' clicks and take appropriate actions.
Also, I would like to display a check mark on the selected item. How can that be done ?
Upvotes: 3
Views: 4062
Reputation: 13761
It works the same way as the onClickListener
, just instead you're checking the onLongClickListener
. So you'd have this kind of structure:
your_view.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
...
}
});
If you want to display a check mark, in my opinion the best way is defining your own row layout, where you just define a CheckBox
at the right side the content of the row. This way, instead of passing the ArrayAdapter
some Android layout, you'd specify your new layout, something like:
your_adapter = new ArrayAdapter(context, R.layout.your_new_layout, initial_rows);
Upvotes: 2
Reputation: 6612
Simple : OnLongClickListener
Then you need to manually remember what is selected or not. You need to notify the list from the changes and do something in the getView method of you adapter.
It would be a good practice to use the Contextual ActionBar mode to interact with all item at once, see here.
Upvotes: 2
Reputation: 227
Answered in https://stackoverflow.com/questions/12090394/i-cant-get-longclick-to-work-on-listactivity :
// Optional, added if done from ListActivity (or possibly ListFragment)
ListView lv = getListView();
// Set on this if overriding OnItemLongClickListener, otherwise use an anonymous inner function
lv.setOnItemLongClickListener(this);
Upvotes: 2