I have two spinners that I want to be "tied" to one another in a mutually-exclusive sense: If you select an item in one, that item's text turns red and appears at the top, while the other one goes back to displaying the initial ("title") selection (if another item was previously selected), and its text turns white.
This is all done via the onItemSelected listeners:
sectionSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
issueSpin.setSelection(0);
((TextView) issueSpin.getChildAt(0)).setTextColor(Color.parseColor("#FFFFFF"));
((TextView) arg1).setTextColor(Color.parseColor("#E3170D"));
....
and vice versa for the "issue spinner". My problem is that, if I'm going from one spinner to the other and I select the top item, the onItemSelectedListener doesn't register because the item being selected is already selected.
I've been told that this is not possible. Or, rather, I've been told that it is impossible for an onItemSelected listener to fire on an item that is already selected. While I realize this is technically true, this problem seems relatively simple and I'm sure there must be a workaround to produce the desired effect.
I have a few questions regarding some that I'm pondering:
onItemSelectedListener
? onItemSelectedListener
?Instead of using a Spinner
, use a Button
that visually resembles a non-active Spinner
. When the button is clicked, show a custom Dialog
containing a ListView
. You can use the setSelection
method and/or the position in the Adapter
's getView
to show which item is currently selected. The ListView
's OnItemClickListener
will get notified that something got clicked regardless of whether the item was selected or not.
When you find which item was clicked, hide the Dialog
and notify the button-containing Activity
of this, so that it can update both Buttons
if needed.
I have implemented something really similar to this using DialogFragments
and a DialogFragmentParent
and it works fine.
Have you consider something like below,
sectionSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
updateView("sectionSpin");
}
}
And for other one
issueSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
updateView("issueSpin");
}
}
and now
private void updateView(String spinner){
if (spinner.equals(sectionSpin)){
//update spinner per requirement
}
else{
//update spinner per requirement
}
}
You can also consider some additional variable or parameters to keep track if the item selected for first time or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With