Android pass value to Spinner - android

I have to pass a value in the sqlite db spinner of another activity.
the value of the step as follows:
extras.putString ("category", tv4.getText().toString ());
But then as I insert it in the spinner?
spinner.set ...... (i.getStringExtra("category"));

Presumably your spinner has an adapter and an array that fills that adapter. You need to first find the index of the string you are passing in from the array. Then you will set the spinner's position to that index.

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

Get the respective value from the array according to selected item position

I have a spinner and I'm getting its selected item position by MyOnItemSelectedListener. By the time I'm using an array adapter to load items to the spinner. I have loaded the items and it works perfectly. But I have a small problem. When I doesn't select a value then it shows position 0 but it has got the value of 1st array value. As shown in the below image,
What I want to do is when I haven't select a value then it should get any value from array. And when I select 1st item then only it should get 1st array value.
Array is a created from json response so it is not possible to add a item to array manually.
I have used How to make an Android Spinner with initial text "Select One" (aaronvargas's answer) to add slect option to spinner as 1st selection.
How can I achieve this? Any help will be highly appreciated.
You can use the logic like :
int selectedValue = -1;
if(position<=0){
//Means Item not selected
}else{
selectedValue = array[position-1];
}
I think you can use something like I have shown.
if(position==0){
variable = 0;
}else{
//use ur logic here
}

Listview Row incrementing with new value

I have a listview with radio buttons now i need to add one value from a edittext after typing some thing in the edittext the typed text should appear in the listview as next row.? how it is posible.?
ListView with simpleArrayAdapter :
Follow these steps it may help:
1. Fetch the value from edit text after the user types using gettext()
2. Insert this value in an array.
3. create a list view.
4. create an arrayAdapter with the already created array as source.
5. Now set the adapter with listview using (array variable).setAdapter(adapter name);
For every time the user updates use notifyDataSetChanged() to refresh your listview with the new contents.
Pseudo code:
onClick(){
array.add(editText.gettext());
adapter.notifyDataSetChanged();
}
Click here for more.

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

Android : Set a value from an external file in a spinner

I'm stuck on a problem with spinners.
Actually, I have some tabs with spinners and EditText that I create without any porblems. I have to keep the datas that the user type in an xml file. So I create an xml file and it workd fine.
I also have a loading tab that permis to load datas saved in this xml. So I load the file, I parse it and I fill my EditTexts without any problems.
The problem is from the spinner : I can't put the data from the XML I created in my spinner.
I tried by saving it as a string and then I tried to load it in the spinner by this way :
(Spinner) spinner.setPrompt(string);
That doesn't work, I have the default value but not the saved value.
I also tried to save the integer of the choice made by the user. And then to reload it by this way :
(Spinner) spinner.setSelection(Integer.parseInt(string));
I don't think I use it properly cause I have a FC.
So I don't really know how to proceed to load the value from the xml in my spinner.
Any idea ?
Thanks !
Try this:
String myString = "some value"; //the value you want the position for
ArrayAdapter myAdap = (ArrayAdapter) mySpinner.getAdapter(); //cast to an ArrayAdapter
int spinnerPosition = myAdap.getPosition(myString);
//set the default according to value
mySpinner.setSelection(spinnerPosition);
From:
How to set selected item of Spinner by value, not by position?

Categories

Resources