Xamarin spinner firing itemSelected event multiple times - java

I am writing some code in Xamarin but I assume my issue would be similar in Java.
In my main activity I have few spinners and I want to trigger some action when an item is changed.
Suppose the spinner holds a list of names and the action to perform when an item is selected is a toast showing the name I selected in the spinner.
When I first launch the app, the spinner is populated with some names and I put the following line to trigger the toast when an item is selected
spinNames.ItemSelected += SpinNames_ItemSelected;
and the method
private void SpinNams_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
// toast message with the selected name
}
the problem is that this event is fired multiple times cause actually there is always an item selected.
How can I make sure that the event is only fired the very first time (when the activity is launched, so the spinner is populated and shown the first time) and then only when there is a new item selected in the spinner.
So that the event is only fired once when I actuallymake a new selection in the spinner.
I thought I could use some global variable to store the current selection so that the firing of the event can be conditioned to the item selected in the spinner to differ from what the global variable stores.
But seems too complex since I might have many new global variables to handle when I have multiple spinners. Any smarter way to achieve my goal?

Related

Spinner inandroid studio

I have 2 spinners and 1 edit text. How to get selected spinner item. I need to know witch ones are selected, so I could show conversion resoult in other activity.
First spinner have 2 items ( Celsius and Fahrenheit) second also. How to call one of 2 methods depending on spinner item select.
Create a reference for your spinners. Extract the selected value of the spinners like this:
String spinnerValue = String.valueOf(spinnerReference.getSelectedItem());
Considering that spinnerValue can contain either "Farenheit" or "Celsisus", you can use a switch block to call the appropriate method.
If you want to check currently selected item in the spinner you should have an instance of it. If you don't make sure you assign an id (for example "unitSpinner") to it in the layout file and then in the Activity's onCreate method call Spinner spinner = (Spinner) findViewById(R.id.<idYouAssigned>). Then you can call spinner.getSelectedItem() which will return either "Celsuius" or "Fahrenheit" depending on which one the user selected.

How to make the app wait until something (until variable changes) happens

I created a main class and an adapter for my ListView. In this adapter I created three different types of rows. One of them is a row with EditText. In the adapter class I created a Listener for "Enter" click. After an user clicks "Enter" button, the information entered in EditText saves in a variable.
What I need: I want my app (my main class) wait until this variable changes (from null to something) and after continue doing some actions.
I tried to create a loop, but if ended up with error.
Try to use onEditorDoneListener on the edittext(example is here) when the user presses done button on the keyboard, you can verify the value of the editText and proceed accordingly.
After an user clicks "Enter" button, the information entered in EditText saves in a variable.
It seems that this variable (presumably a String) is only ever changed when the "Enter" button is pressed. So whatever action you want when that String is changed, put them into the onClick method in your Java:
private void enterButtonOnClick(View v) {
// Store the text from EditText into the String variable here
// ...
// Your string variable just changed, so do the desired action...
// Stuff
// and
// things
}
And then in your XML, add this in the button attribute :
android:onClick="enterButtonOnClick"

Android: when spinner item is selected how to prompt for additional details

Trying to make a checklist type app that when you select an element from a spinner you will be prompted for additional information. For instance if you select apple it will ask you how many and give you an editable text box to select.
Should I design the page so that it uses a new intent after each item is selected and creates a new page with the prompting and previous selections in it?
Or is there a better method?
OnItemSelectedListener and AlertDialog is the way.
1.Use OnItemSelectedListener and override the ItemSelected()
2.create and show the AlertDialog with edittext inside the ItemSelected method.
If you are not taking so many inputs from user then you can create a simple AlertDialog for getting those values on every item selected from Spinner or if there are lot of inputs needed then you should call a separate activity for that.

Android Spinner value retrieved from database and dynamic selected item

Hi All I need your help.
I am currently having problems regarding to Android spinner.
Well, I have two spinners, one for source language and the other for target language. these two spinners values contain language list retrieved from database. For example in database I have list of language:
language_code(en,kr,jp,chinese), description (English,Korean,Japanese,Chinese).
The first spinner will contain all the language description whereas the second one will contain all except the selected one in the 1st spinner.
if the selected item in the 1st spinner is English then the values in 2nd spinner must be only the rest exclude English.
What I want is the user can change the languages any time they want, so when the spinner value is changed, user have to click a button to trigger the change and then using the intent to pass to the same class but with the different spinner value. And what I want is the spinner selected item for source and target are from the previous selection.
I have done with the spinner but I have idea with the dynamic change. Any idea?
Thank you

Android, show dialog when ListPreference item is clicked

Basically I have a ListPreference to allow a user to change the X position of some text on my Live Wallpaper.
It contains 4 entries: top, middle, bottom and manually input X. The first 3 options are no problem, I simply get the SharedPreferences in my WallpaperService class and check if they are top, middle or bottom and change the position corresponding to their choice.
However, the last option is proving more difficult, what I want to do is have an EditText alert box popup when the user clicks the "Manually input X" ListPreference item so they can enter a value for X. I just cant figure out how to make the alert popup from clicking that specific List element.
You probably want to create a custom ListPreference. Basically you want to extend from ListPreference (see original here), and provide a custom protected void onPrepareDialogBuilder(Builder builder), in which you provide the additional "custom" list item and the onclick to handle the selection of the "custom" entry.
Note that I keep saying "custom" because it would be a best practice to make this class as reusable as possible.
Override onPreferenceTreeClick() in your PreferenceActivity and compare the preference it gives to the one you want to do something for.

Categories