Reputation: 20223
I am a novice on Android developpement and I need some help.
Before i start with my question, thank you for your help.
Ok, here is my problem:
I have a listView with CHOICE_MODE_MULTIPLE enabled. With this, i can select some elements on my listView.
I would like to use an item selection as an Event in my code, for exemple, when the item is selected, i would like to display a toast (Item "Name of Item" Selected).
I don't know how to do this, i have read something about the onSelectedItemListener but I don't know how to implement and use it.
Thank you again for all your help.
Here is the solution for this problem: I made a list with the selected items. To see which itel is selected, i have to test if the id of this item is it the list. If insside, the item is selected, if not, the item is not selected.
listeCapteurs.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (!mSelectedIndices.contains(position))
{
mSelectedIndices.add(position);
System.out.println("Liste:" +mSelectedIndices );
}
else
{mSelectedIndices.remove((Integer)position);
System.out.println("Liste:" +mSelectedIndices );
}
Log.i(null, "Clicked item position = " + position );
}});
}
Upvotes: 3
Views: 4156
Reputation: 45503
Have you had a read through the 'Hello ListView demo'? It demonstrates how to use the OnItemClickListener
. Now, that might not be exactly what you're after, but from an implementation point of view, adding an OnItemSelectedListener
is identical:
listview.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// do something...
}
}
Edits as per comments below.
You could try explicitly setting the selection to a specific item after populating the list to see if that triggers OnItemSelected. Assuming you have at least two items in your list:
listview.setSelection(1); // select second item
//Edit2: Actually, this will probably not make a difference, reading the documentation for setSelection.
Sets the currently selected item. If in touch mode, the item will not be selected but it will still be positioned appropriately. (...)
Alternatively you can keep track of the selected items yourself using the OnItemClickListener, assuming there's no other action upon clicking an item - which makes sense on devices with only a touch screen, as there is no way to differentiate between a 'selection' and 'click'.
List<Integer> mSelectedIndices = new ArrayList<Integer>();
//...
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (!mSelectedIndices.contains(position)) {
mSelectedIndices.add(position);
} else {
mSelectedIndices.remove((Integer)position);
}
}
}
I'm guessing that at some point you want to do something with the selected items, so then you can just iterate over above list and grab the objects that were clicked/selected.
Upvotes: 1
Reputation: 26991
It should look something like this..
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
Upvotes: 0