why onItemSelected() not return "null" value when nothing selected from spinner - android

in my app i have a spinner and a button below it when i run the program suppose this page is open which contains spinner and a button ,when i dont select any item from spineer it takes first values of spinner by default i want if nothing is selected it contains null value ..... how to do this??? thankss a lot my code is..

HI Saurabh,
I would suggest you to give your first spinner value as "--Select--" or some message. And try to validate in your code, by checking return value of the spinner which should not be the first manual value that you given into the spinner.
You can refer this page
http://developer.android.com/resources/tutorials/views/hello-spinner.html

the parameter "position" records your selected item.so you can use this parameter.

Related

How to set a particular position as default in spinner?

I am getting list of data from server and setting in spinner through setAdapter, but what data is coming on 3rd position I want to set that as default(0th position). Ex. {Mango, Banana, apple} ; in spinner apple should be default instead of Mango
else if
(mListener.getSelection().get(0).
getGenLovs().get(i).getLovId().
equalsIgnoreCase(File_Key.AB_CUST_TITLE))
{
binding.spinTitle.setAdapter(new
GenLovsSpinner(getContext(),
mListener.getSelection().get(0).
getGenLovs().get(i).getValDes()));
}
I have tried this
String cls=
String.valueOf(mListener.getSelection().
get(0).getGenLovs().get(i).getValDes().get(3));
binding.spinTitle.setSelection(Integer.parseInt(cls),true);
Here when I am using above code I am getting NumberFormatException
binding.spinTitle.setSelection(Integer.parseInt(cls),true);
use this insted of above line
binding.spinTitle.setSelection(Integer.valueOf(cls));
See you are setting up any list or array to spinner adapter.
If you want to set particular as default then try this for example :-
Let you are setting dataList to spinner adapter
after setAdapter() for selection
either spinner.setSelection(dataList.indexOf("apple"),true) or
spinner.setSelection(2,true) as your third data has index 2
Just give binding.spinTitle.setSelection(2);
try this
Use the following: spinnerObject.setSelection(position).

How to get certain field from the arrayList object on spinner selection

I just created a spinner and make this spinner read from the database. and display them in the spinner. I already did that. But I want to right down the Id of the selected item.
for example : when I select the first raw called (1, tyat abdullah, sergurey )
I need the Id (1) only to be written down without the full record.
I need the Id (1) only to be written down without the full record.
Then when you are doing labels.add, only put the data you want to see
You can also use doctorsList.get(position) within the selection listener, and set the other text view accordingly.
For example, doctorsList.get(position).getId() seems to get the information you are asking for
simply use this to get the selected item index
int index = spinner.getSelectedItemPosition(); //this will give you the seleected item index
is that what you want ?
leme Know :)

How to change the text of a radio button based on the selection in the spinner which are both within a fragment

I have a spinner which holds the years as ranges.
fromYear-toYear
For example:
2015-2016
2016-2017
I need to check if the toYear is a leap year on selection and based on if it is leap year or not i need to change the text of radio buttons in a group placed below the spinner.
There are 3 radio buttons within the group.
Thanks in advance for the answer.
First you must get selected item String from your Spinner , to do that you must check setOnItemSelectedListener method and when user choosed one item from your spinner you get the selected item String value try this code to get it :
String Text = mySpinner.getSelectedItem().toString();
Then you must check text value to get ToYear from it .
In Java you can user substring() method.
For example if selected item text equals 2015-2016 :
text.substring(5,4);
This will return you 2016 and you can do something you like by checking it ,
You can change start and end index if it is't returned true value . this is the standard type of substring usage :
str.substring(startIndex, endIndex);

how to make a selected value in spinner as a default one for next session in android

I am working on an android project and I am using a spinner which is populated manually.
For example if, in the spinner I have the following items:
select
Category 1
Category 2
Category 3
Initially the spinner value is select now i selected category 2
How would I programmatically make Category 2 as the default value for next session(opening the app next time).
Thank you.
In the first session you save the selected index in a SharedPreferences:
PreferenceManager.getDefaultSharedPreferences(this).edit().putInt("srIndex",spinner.getSelectedItemPosition());
And in the second session you read the saved index and set it to the spinner:
spinner.setSelection(PreferenceManager.getDefaultSharedPreferences(this).getInt("srIndex",0));
If you know the position of "Category 2" you can do the item below. You can get the index from the adapter.
spinner.setSelection(indexOfCategory2);
You can use SharedPreferences to save the index of selected category and every time activity is loaded retrieve the index from SharedPreferences and set selection
Spinner.setSelection(index);
If you not aware of SharedPreferences you can refer to StackOverflow

how to set spinner selection by text inside it

I preparing a form in which i have to use same page for Adding details and Editing details. While adding details all fields will be blank and spinner selection will be set to "no selection".
Now i want to set the spinner selection of the item which i am going to pass from the previous activity. How to achieve this ??
As spinner does not have any method something like, setSelection(String string);
Or is there any other way, i can achieve this mechanism...
Would anyone please help me...
This is what i did and it seems to work fine
Spinner my_spinner=(Spinner)findViewById(R.id.spn_items);
ArrayAdapter<String> array_spinner=(ArrayAdapter<String>)my_spinner.getAdapter();
my_spinner.setSelection(array_spinner.getPosition("list item"));
I dont now how frequently this might be used but we can set selection of the spinner by text inside it.
Spinner has the method setSelection(int position);.
Now in the parameter we need to pass position of the text, which we can get from the array_list we use to bind to adapter, by getIndexOf(Object object) and object should be of the type of ArrayList that is declared For example, if ArrayList is of type String, the object to be passed to getIndexOf(Object object) should be of type String.
Finally, you set selection as below:
spinner.setSelection ( spinner_array_list.indexOf(string) );

Categories

Resources