Spinner inandroid studio - java

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.

Related

android textfield programatically based on data

I am Having a trouble. In my project i am having a spinner . Each value i will select in spinner it will make an api call which in return is receiving various objects array.
Important : Each value in spinner will get data from api consisting of different array length.
What i want is to display that each value of array in a spinner .The thing is i am not finding any way to create only that amount of spinner which are equal to data in array.
Example :Spinner value is 1 and array we get from api call has 3 values. Then if spinner value is 2, array we get has 5 values. I have to create 3 spinners if spinner value is 1 and 5 spinners on layout if spinner value is 2 automatically.
Note : Array values can go upto 15 .
Use a RecyclerView for dynamic content. When you get different array from api change you Adapter data source and notify for Adapter for data change .
Check the link i provided, there is a Adapter class called MyAdapter. You just have to update mDataset String Array and need to call notifyDataSetChanged. Adapter will automatically update its resource.
I would suggest you just create one textview and use StringBuilder.append to append all values from the array with proper styling (meaning commas etc)

Identify a view used in multiple activities

I have a spinner that i use over several activities. In one of them, I have to modify one of the parameters of the spinner. How could i identify from the spinner itself (without passing any extra parameter) the activities where the spinner will be inflated?
Depends a bit on your app - but I see 2 approaches for now:
1 You could add a tag to the spinner
2 You check the context of the spinner

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.

Setting the selected item in a Spinner widget

In my Android app, I have an Activity with a Spinner widget. I created it with the following code in onCreate():
this.playerPositionSpinner = (Spinner) this.findViewById(R.id.player_position_text);
ArrayAdapter<CharSequence> positionsAdapter = ArrayAdapter.createFromResource(this, R.array.positions, android.R.layout.simple_spinner_item);
positionsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.playerPositionSpinner.setAdapter(positionsAdapter);
Later I want to set the selected item based on a String which I know is a valid item in the Spinner's list? All of the Spinner.setSelection() methods take an int parameter for the position. How do I obtain the position of a String in the Spinners drop-down list?
(You have posted some code now! Yeah! This simplifies my example drastically.)
I recommend using Spinner#setSelection() after calling setAdapter():
playerPositionSpinner.setSelection(positionsAdapter.getPosition("Watermelon"));
Obviously you can replace "Watermelon" with something relevant for your app.
Use your spinner setSelection method:
http://developer.android.com/reference/android/widget/AbsSpinner.html#setSelection(int, boolean)
You need to pass it the integer value of the location of the item. to get the value use your adapter.
You'll need to call playerPositionSpinner.setSelection(position) after creating and setting the Spinner's ArrayAdapter

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

Categories