Long click to begin selecting items in a ListView? - android

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 ?

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.

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);

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);

Related

custom listview save selected item position

I create a custom listview.
I want to select any item using another button click or something views click.
Already I tried via these code for custom selection.
And I also want to get previous selected item on resume.
listView.setItemChecked(2,true);
listView.setSelection(2);
listView.requestFocus();
But everytime , I failed.
I used this method to create custom listview.
Android ListView with Custom Adapter Example Tutorial
Advance Thanks .
For setItemChecked to work, your list items need to be Checkable; you can, for example use CheckedTextView (at the top level of the item view, no further layout around it).
For setSelection to work, you need to use it inside a Runnable, like so:
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(2);
}
});
In both cases, don't ask why.
For a selection to become visible, you can put a statelist in the background attribute of your list item.

Android setOnItemClickListener vs setOnClickListener

As I know, there are two methods to handle click on different list items:
Use setTag() to set types for items of list in Adapter, then setOnItemClickListener() for the list and getTag() of the view to differentiate the type, like this:
listview.setOnItemClickListener(new OnItemClcikListener(){});
Inside the adapter, setOnClickListener() individually for each item during getView(), like this:
item.setOnClickListener(new OnClickListener() {});
What is the difference and which one is preferred?
OnItemClickListener is very easy to manage compare to OnClickListener.
If you still would love to manage OnClickListener I will tell why OnItemClickListener is much better than OnClickListener.
Once you start scrolling ListView items will start getting reused and you ended up creating lot of OnClickListener. Don't worry, this is not a memory leak as GC will come into picture and collect those but you should not feel safe also because GC pauses your activity even if it is fraction of second that's considerable.
So would I go with OnItemClickListener unless you planned something different for individual list item.
If you need to create specific portions of each item to be clickable or want more than one action to be performed for a given item, it would be best if possible to collect those actions into a single OnClickListener that is created once and then attached to each item in getView(). You can differentiate which item was clicked by attaching metadata about the click action and maybe list position to the views themselves with setTag().
I'm not sure i really understood what do you mean but i will try to give you a reply.
Use setTag() to set types for items of list in Adapter, then setOnItemClickListener() for the list and getTag() of the view to differentiate the type, like this:
listview.setOnItemClickListener(new OnItemClcikListener(){});
You can use setTag() for set an Object like a tag, it means you can use it to put some informations into your cell view (ex: text, id and so on).
To "differentiate the type" of your view i would suggest you to use `getViewTypeCount().
inside the adapter, setOnClickListener() individually for each item during getView(), like this:
item.setOnClickListener(new OnClickListener() {});
This actually depends more on what you want to do with your list, if the behaviour of the click is strictly related to the informations which belong to your adapter or for example if you have a button inside your cell view, then set a listener for the cell view inside the adapter could be a good solution.
But except for the last case i would say the first is the best solution, since you can put everything you want into your tag, and it gives you the chance to manage your list's click from your main Activity or Fragment.

Android ListView with CheckBox and a selectable item

I have seen plenty of samples which support a checkbox for each item in a list but this seems to be about as far as they go. My problem is that I have two lists of items which interact.
The first list displays Method Statements - when the user select a method statement the second list displays a list of Risk Assesments for this method statement. I have this all working with no issues.
I now need to extend this so that the user can select (via a checkbox) the Method Statements that they want to use. What this means is that I do not want the selection of the listitem to set or clear the check box, just populate the second listbox. I do however need the user to be able to manually check the item to include it.
Any suggestions please would be most welcome - maybe a ListView is not the correct way to do this, perhaps a dynamically created table within a scroll view?
I would try this way.
Set choice mode of ListView as NONE.
Build a ViewGroup (Linear/Relative/Frame Layout) with TextView and CheckBox. It will
be a cell of ListView.
When you bind view in the adapter, set CompoundButton.OnCheckedChangeListener on the CheckBox. Place position of your view in the Integer and set it as a tag for your cell (remember: do it in the binding data with view).
private static class MyOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
#Override
public void onCheckedChange(...) {
Integer position = (Integer) view.getTag();
//your logic with position
}
}
Or just set OnClickListener.
The idea is: let CheckBox be clickable and let it consume click events - then its parent will not receive this events and AdapterView.OnItemClickListener will not be fired.

Preselect items in listView Android

All i need is an idea or path to continue searching, i have a list of items that i add to a listview adapter. And i am trying to make some items in the list to have a specific background color (as an example) when the activity loads. Something like having those rows "preselected". The method public ListView getListView() does not help me much.
Thank you either way.
Need to set Multiple Choice Mode via setChoiceMode(), and then you can call setItemChecked() for each of the items you want checked. Then you probably have to make a custom adapter that overrides getView(), cast the parent to ListView and ask if the View (a list item) you are getting at the position given is in fact checked by calling something like getCheckedItems() on the parent. Then you can do what ever you want with that view, like set its background color etc.

Are the ListView equipped of some system to enable clicked item to stay selected?

As the title say.. Are the ListView equipped of some system to enable clicked item to stay selected ?
Thanks.
Besides, focus there is no such thing. You can improvise it by setting color to the item.
Unfortunately not.
Here is an answer previously provided:
Highlight ListView selected row
Because of the way the Android Framework is designed with Touch Mode, there is no way to do so without creating a custom ListView and programatically setting the background to a different color. You can keep track of your "last selected view" either through an id, a position, or a reference to the view itself.
Another option is to do this in a custom adapter's bind view, although this is more of a hassle because all your views have to be rebuilt in order for it to work properly.
I have read somewhere that they are to fix this issue in the Honeycomb though.
Sorry guys but i found a way to do so and it work fine. It was exactly what i was looking for. To help others i will post an exemple.
First, you have to implement the mode in the layout with android:choiceMode="singleChoice".
You need to specify a multi-choice list in the adapter.
ArrayAdapter adapter = new ArrayAdapter(monContext,
android.R.layout.simple_list_item_single_choice, maListePrenoms);
3.Then in the code you can get the value of the selected item...
btnAfficherNom.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(monContext, lstPersonnes.getItemAtPosition(lstPersonnes.getCheckedItemPosition()).toString(), Toast.LENGTH_SHORT).show();
}
});

Categories

Resources