I'm building a simple App with a ListView in it. In the Java code, I'm trying to figure out which item(s) is currently selected. From the ListView documentation at http://developer.android.com/reference/android/widget/ListView.html I see there's a method in the ListView class called
public void setSelection (int position)
I looked for a similarly named method named getSelection(), but there is none.
So, first, how do I determine which item in my ListView is currently selected? I already have the object for the ListView, so I just need the method to tells me which item the user clicked on.
ListView myLV = (ListView) findViewByID(R.id.myListView);
Also, can I define a "callback" function which is called whenever an item is selected?
Finally, how can I use the documentation to figure this out? This is a very basic question which I should be able to figure out on my own. I did what seems to be the right thing: I looked at the Official Developer pages for the ListView class, but it didn't provide the answer. How can I answer these (basic) questions for myself?
Try this
mylv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View v, int position, long lng) {
String selectedFromList =(String) (mylv.getItemAtPosition(position));
}
});
I looked for a similarly named method named getSelection(), but there is none. So, first, how do I determine which item in my ListView is currently selected?
The super class AdapterView has getSelectedItem(), along with getSelectedItemId() and others. AbsListView has getSelectedView() too. But these only tend to work when you have used setChoiceMode()
The OnItemClickListener lets you know which it is clicked as it is clicked.
Finally, how can I use the documentation to figure this out?
You were right to start in ListView's documentation, but go to the "Inherited Methods" section and check the super classes as well.
You can get the position by using onitem click listener.by using on item click listener you can get position of selected item in the list.And you can put this position in Toast
I'm implementing a custom calendar using a GridView.
For this Calendar I have 3 view types, {DAY, TITLE, BLANK}
Is there a way to make certain items in a GridView not clickable?
What I'm trying to avoid is the press state animations for the items that aren't clickable. It ok it the user presses them, I can recognize that it's not the valid view in my onItemClickListener(). This is purely for UI purposes.
I found the solution. Don't know why I didn't search for ListViews in the first place.
You need to override the isEnabled() Function
Returning false will make it non-clickable. True will keep is clickable.
#Override
public boolean isEnabled(int position) {
switch(getItemViewType(position)) {
case CellTypes.BLANK:
case CellTypes.TITLE:
default :
return false;
case CellTypes.DAY:
return true;
}
}
In the adapter for your GridView, implement one of the get() method to return a reference to the object being clicked. I'm assuming that your adapter wraps a list of your "Calendar" objects.
In the onItemClickListener for your GridView, call get(index) or get(id) on the adapter to get a reference to the object being clicked. Check its type to see if it is one that you do not wish to be clickable, and return before executing the logic that is usually called when a clickable item is clicked.
In XML: android:clickable="false" and in code: setClickable(false);
It's taken me quite a while to get my head around the Android Spinner. After several failed implementation attempts, and after reading many questions partially similar to my own but without satisfactory answers, and some without any answers at all, e.g. here and here, I finally get that a "spinner" in Android isn't meant to be the same thing as a "drop-down list" from desktop apps, or a select in HTML. However, what my app (and I'm guessing the apps of all the other posters whose questions are similar) needs is something that works like a drop-down box, not like a spinner.
My two problems are with what I first considered to be idiosynchrasies the OnItemSelectedListener (I've seen these as separate questions on this site but not as one):
An initial selection of the first list item is triggered automatically without the user's interaction.
When the item that was already selected is selected again by the user, it is ignored.
Now I realise that, when you think about it, it makes sense for this to happen on a spinner - it has to start with a default value selected, and you spin it only to change that value, not to "re-select" a value - the documentation actually says: "This callback is invoked only when the newly selected position is different from the previously selected position". And I've seen answers suggesting that you set up a flag to ignore the first automatic selection - I guess I could live with that if there's no other way.
But since what I really want is a drop-down list which behaves as a drop-down list should (and as users can and should expect), what I need is something like a Spinner that behaves like a drop-down, like a combo-box. I don't care about any automatic pre-selection (that should happen without triggering my listener), and I want to know about every selection, even if it's the same one as previously (after all, the user selected the same item again).
So... is there something in Android that can do that, or some workaround to make a Spinner behave like a drop-down list? If there is a question like this one on this site that I haven't found, and which has a satisfactory answer, please let me know (in which case I sincerely apologise for repeating the question).
+1 to David's answer. However, here's an implementation suggestion that does not involve copy-pasting code from the source (which, by the way, looks exactly the same as David posted in 2.3 as well):
#Override
void setSelectionInt(int position, boolean animate) {
mOldSelectedPosition = INVALID_POSITION;
super.setSelectionInt(position, animate);
}
This way you'll trick the parent method into thinking it's a new position every time.
Alternatively, you could try setting the position to invalid when the spinner is clicked and setting it back in onNothingSelected. This is not as nice, because the user will not see what item is selected while the dialog is up.
Ok, I think I've come up with a solution for my own situation with the help of both David's and Felix' answer (I believe David's helped Felix', which in turn helped mine). I thought I'd post it here together with a code sample in case someone else finds this approach useful as well. It also solves both of my problems (both the unwanted automatic selection and the desired re-selection trigger).
What I've done is added a "please select" dummy item as the first item in my list (initially just to get around the automatic selection problem so that I could ignore when it was selected without user interaction), and then, when another item is selected and I've handled the selection, I simply reset the spinner to the dummy item (which gets ignored). Come to think of it, I should've thought of this long ago before deciding to post my question on this site, but things are always more obvious in hindsight... and I found that writing my question actually helped me to think about what I wanted to achieve.
Obviously, if having a dummy item doesn't fit your situation, this might not be the ideal solution for you, but since what I wanted was to trigger an action when the user selected a value (and having the value remain selected is not required in my specific case), this works just fine. I'll try to add a simplified code example (may not compile as is, I've ripped out a few bits from my working code and renamed things before pasting, but hopefully you'll get the idea) below.
First, the list activity (in my case) containing the spinner, let's call it MyListActivity:
public class MyListActivity extends ListActivity {
private Spinner mySpinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: other code as required...
mySpinner = (Spinner) findViewById(R.id.mySpinner);
mySpinner.setAdapter(new MySpinnerAdapter(this));
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> aParentView,
View aView, int aPosition, long anId) {
if (aPosition == 0) {
Log.d(getClass().getName(), "Ignoring selection of dummy list item...");
} else {
Log.d(getClass().getName(), "Handling selection of actual list item...");
// TODO: insert code to handle selection
resetSelection();
}
}
#Override
public void onNothingSelected(AdapterView<?> anAdapterView) {
// do nothing
}
});
}
/**
* Reset the filter spinner selection to 0 - which is ignored in
* onItemSelected() - so that a subsequent selection of another item is
* triggered, regardless of whether it's the same item that was selected
* previously.
*/
protected void resetSelection() {
Log.d(getClass().getName(), "Resetting selection to 0 (i.e. 'please select' item).");
mySpinner.setSelection(0);
}
}
And the spinner adapter code could look something like this (could in fact be an inner class in the above list activity if you prefer):
public class MySpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
private List<MyListItem> items; // replace MyListItem with your model object type
private Context context;
public MySpinnerAdapter(Context aContext) {
context = aContext;
items = new ArrayList<MyListItem>();
items.add(null); // add first dummy item - selection of this will be ignored
// TODO: add other items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int aPosition) {
return items.get(aPosition);
}
#Override
public long getItemId(int aPosition) {
return aPosition;
}
#Override
public View getView(int aPosition, View aView, ViewGroup aParent) {
TextView text = new TextView(context);
if (aPosition == 0) {
text.setText("-- Please select --"); // text for first dummy item
} else {
text.setText(items.get(aPosition).toString());
// or use whatever model attribute you'd like displayed instead of toString()
}
return text;
}
}
I guess (haven't tried this) the same effect could be achieved using setSelected(false) instead of setSelection(0), but re-setting to "please select" suits my purposes fine. And, "look, Ma, no flag!" (Although I guess ignoring 0 selections is not that dissimilar.)
Hopefully, this can help someone else out there with a similar use case. :-) For other use cases, Felix' answer may be more suitable (thanks Felix!).
Look. I don't know if this will help you, but since you seem tired of looking for an answer without much success, this idea may help you, who knows...
The Spinner class is derived from AbsSpinner. Inside this, there is this method:
void setSelectionInt(int position, boolean animate) {
if (position != mOldSelectedPosition) {
mBlockLayoutRequests = true;
int delta = position - mSelectedPosition;
setNextSelectedPositionInt(position);
layout(delta, animate);
mBlockLayoutRequests = false;
}
}
This is AFAIK taken from 1.5 source. Perhaps you could check that source, see how Spinner/AbsSpinner works, and maybe extend that class just enough to catch the proper method and not check if position != mOldSelectedPosition.
I mean... that's a huge "maybe" with a lot of "ifs" (android versioning comes to mind etc.), but since you seem frustrated (and I've been there with Android many times), maybe this can give you some "light". And I assume that there are no other obvious answers by looking at your previous research.
I wish you good luck!
Here is an alternative solution to differentiate between any (intended or unintended) programmatic and user-initiated changes:
Create your listener for the spinner as both an OnTouchListener and OnItemSelectedListener
public class SpinnerInteractionListener implements AdapterView.OnItemSelectedListener, View.OnTouchListener {
boolean userSelect = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
userSelect = true;
return false;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (userSelect) {
// Your selection handling code here
userSelect = false;
}
}
}
Add the listener to the spinner registering for both event types
SpinnerInteractionListener listener = new SpinnerInteractionListener();
mSpinnerView.setOnTouchListener(listener);
mSpinnerView.setOnItemSelectedListener(listener);
This wouldn't handle the case in which the re-selection of the same item by the user doesn't trigger the onItemSelected method (which I have not observed), but I guess that could be handled by adding some code to the onTouch method.
Anyway, the problems Amos pointed out were driving me crazy before thinking of this solution, so I thought I'd share as widely as possible. There are many threads that discuss this, but I've only seen one other solution so far that is similar to this: https://stackoverflow.com/a/25070696/4556980.
Modifying the Spinner is useful if you want to have multiple selections simultaneously in the same activity.
If you only desire the user to have a hierarchical selection, for example:
What do you want to eat?
Fruit
Apples
Bananas
Oranges
Fast Food
Burgers
Fries
Hot dogs,
then the ExpandableListView might be better for you. It allows the user to navigate a hierarchy of different groups and choose a child element. This would be similar to having several Spinners for the user to choose from - if you do not desire a simultaneous selection, that is.
I worked through several of the issues mentioned in this thread before I realized that the PopupMenu widget is what I really wanted. That was easy to implement without the hacks and workarounds needed to change the functionality of a Spinner. PopupMenu was relatively new when this thread was started in 2011, but I hope this helps someone searching for similar functionality now.
I am writing a music player that uses a custom Adapter extending BaseAdapter (efficiency adapter) that I want to display in an AlertDialog using setAdapter() where the user can either click on one of the songs to switch to that position in the playlist OR check songs to remove from the playlist. I tried using a custom click listener so that a user could just long click to remove the item from the list but the listview just doesn't work right... it was removing the wrong items (the ones at the end) even though the ArrayList contained the correct playlist items... (when I removed the item from the ArrayList, I passed it to the adapter which called notifyDataSetChanged... but that just didn't work as I mentioned. There is definitely a bug in the AlertDialog ListView... because there is no reason for it to have popped off the results from the end rather than the correct item.
So... the next method I would like to try is to use the setMultiChoiceItems() method of the AlertDialog... but it appears that it doesn't work with a custom adapter... only simple arrays. Will I have to subclass AlertDialog and Override the setMultiChoiceItems() method or is there a way I can make it work with an ArrayAdapter?
Basically, I can't figure out how to even iterate the list that the AlertDialog creates or whether it even passes that view somehow. In addition, I don't think I can even listen to clicks on checkboxes if I add those to the row. Any help will be greatly appreciated.
EDIT: Asking questions here is like magic... I answered my own question... this is how I did it. I added a hint to each checkbox which is the position of the item in the ArrayList. Then I used OnCheckedChangeListener to capture the selections. When you set a hint it adds text to the checkbox... since the background of the AlertDialog is white (even for clicked items?) I just set the hint text color to transparent.
holder.check.setHintTextColor(Color.TRANSPARENT);
holder.check.setHint(String.valueOf(position));
holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int position = Integer.parseInt((String) buttonView.getHint());
Log.v("onCheckedChanged", "Checked: "+isChecked+" returned: "+position+" which should be "+getItem(position).name);
}
});
Refer This and This
then
Pass a reference to byte[] in setMultiChoiceItems().
final boolean[] booleans = {false, true, false, true, false, false, false};
Then check the value of booleans inside setPositiveButton().
If you need to pass this AlertDialog around, then extend AlertDialog and have create a field boolean as described in 1.
I am new to Android application Development.
How can I identify the mouseover action for a list box? On mouse over of a particular cell I want to highlight that cell or change the background color.
Please help me regarding this.
There isn't a concept of MouseOver in Android, at least one that I know of - user interaction is done through hardware/virtual keyboard and touchscreen.
ListView automatically highlights the current selection, so when you use the up and down DPad keys, you may get the effect you want.
I know this is old but my queries kept returning to this original post so I wanted to share it here.
For what it is worth, implementing the state_hovered in your selector will NOT work for list views on mouse over events.
You can achieve this event using custom array adapter.
*This assumes you know how to create your own selector and ArrayAdapter. If not, you can find those in other Stack Overflow posts.
Create your own array adapter.
Implement View.OnHoverListener in the adapter
Override the 'onHover'method:
#Override
public boolean onHover(View arg0, MotionEvent arg1) {
int ev = arg1.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_HOVER_ENTER:
arg0.setHovered(true);
arg0.setSelected(true);
return true;
case MotionEvent.ACTION_HOVER_EXIT:
arg0.setHovered(false);
arg0.setSelected(false);
return true;
}
return false;
}