how to set spinner selection by text inside it - android

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) );

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 :)

Showing only one parameter of custom object in dropdown menu using AutoCompleteTextView

I want to show only one parameter from object(s) found using AutoCompleteTextView. I have list of custom items and I'm using this list in ArrayAdapter which is used in my AutoCompleteTextView. But as I find item by typing something to AutoCompleteTextView, only object as whole is shown (Object type and some identifier), but I want to show just Objects attribute "name" which is a String.
The way i'd go about this is making a separate arraylist with all the names inside of it. Display that and get the user to chose from there, once they have use the index to find the object in the other list.
Initiate new string array
String[] data = new String[1]); // terms is a List<String>
for(int i=0;i<=1;i++){ //only the 1st position of ur data getting inserted
data[0]=s.get(i).toString();
}
ArrayAdapter<?> adapter = new ArrayAdapter<Object>(activity, android.R.layout.simple_dropdown_item_1line, data);
keywordField.setAdapter(adapter); // keywordField is a AutoCompleteTextView

Getting spinner value without using events

I'm trying to get the value from a spinner (android) without using events, like getting the value from a textbox.
Thanx.
Easy just call up selected item on the Spinner
String Text = mySpinner.getSelectedItem().toString();
You have getSelectedXXX methods from the AdapterView class from which the Spinner derives:
getSelectedItem()
getSelectedItemPosition()
getSelectedItemId()

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

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.

Categories

Resources