I am getting all the values from the webservice. Can we set this value by default in my spinner.like pre populated value for update.
we do like that edittext1.setText("");
So can we do same it for spinner.
please help me for this.
Thnak you
no its not possible in spinner. you can't set a title like value in spinner in starting.We have use ArrayAdaper then it will show value that is selected.
Like Spinner.setSelection(position);
Edited to insert data in spinner
Call these code whenever you want to change content of Spinner
first find it by id
Spinner s = (Spinner) findViewById( R.id.unique_id );
and then place this code where ever you want to update spinner data
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.array_name, android.R.layout.simple_spinner_item );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_drop_down_item );
s.setAdapter( adapter );
R.array.array_name is array store in String file in res.you can use your dynamic array too
Related
How can I create an empty spinner with no elements in it and add them later?
ArrayAdapter<String> aSpin = ArrayAdapter.createFromResource(this, null , null);
Doesnt work.
I'm curious, in which scenario would you want an empty spinner?
anyway, a spinner with a null object will give you an annoying exception. If you really want a spinner "empty", i would recommend you to create a normal spinner with a fake object. That object would be a string like " ". So, it won't be empty but it will look like if it is.
-- Edit --
In order to use a spinner with objects, you will need an adapter. So, first, if you want an empty spinner, just find its view from your layout and that's all. You will have an empty spinner:
mSpinner = (Spinner)findViewById(R.mLayout.mSpinnerID);
Then, when you need to add your items to the spinner, create an array with those items, then add them to the adapter, and add the adapter to your spinner. For example, let's say that you will have strings:
ArrayList<String> mList = new ArrayList<String>();
mList.add("item 1");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mList);
mList.setAdapter(adapter);
An Adapter is something different than a Spinner. If you want to create an empty Spinner, just define one in your layout file or create one programmatically. It will not contain any items if you don't set an Adapter on the Spinner (or specify static entries in the xml declaration).
You might want to take a look at the Spinner tutorial.
Hi I have a spinner for which I would like to change its entry. I have created an array in the values folder. I know that I can edit the entry of the spinner by right clicking on it. But I want to know, how can I change the entry of the spinner using code. I was hoping there would be something like spinner5.editEntries
Can someone help please?
Spinner Spinnermiles = (Spinner) findViewById(R.id.Spinnermiles);
String [] arrmile ={"5","10","20","30","40","50","70","80","90","100"};
adapter = new ArrayAdapter<String>(Searching.this,android.R.layout.simple_spinner_item,arrmile);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinnermiles.setAdapter(adapter);
If you want to change item in spinner at position 3 (which is "30" in example),
Set value at that position e.g.
arrmile[3] = "enter new value you want";
and after that call
adapter.notifyDataSetChanged();
then value at that position will be get updated.
I have placed 4 spinners in a SlidingDrawer. And I have created a string-array in string.xml, like
<string-array name="colorArray">
<item>Red</item>
<item>Green</item>
<item>Blue</item>
<item>Orange</item>
<item>While</item>
<item>Black</item>
</string-array>
I want to populate the spinners with this array..
For that i had done like,
option1 = (Spinner)findViewById(R.id.spinner_first);
adapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.colorArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
option1.setAdapter(adapter);
and the same for the other 3. It works fine for me now. Now the result is like, the entire array is listed on all the spinners.
But i want to remove the item selected by one spinner in all the other spinners, so that that item is not shown in the other 3.. For example, if i select "red" for the first spinner, the item "Red" must be removed from all other spinners..
How can that be achieved.
Sample codes and guidance will be appreciable..
Thanks in advance..
just create the sub array of main array
like first fetch the array from xml file now by default 0th position as selected by default then skipped in sub array
String mainArr[]; // fetch from xml
String sub1[] = new String[mainArr.length-1];
now store the main array value into the sub1 array by iterating and in the get put the condition for storing the value if the selected position == i then skip or selected position!=i do this in item change listener to re create array from the main array with skipping the selected item and notify it by adapter
In your onItemSelected() for the OnItemSelectedListener for the Spinner, you need to do the following for each one of the other Spinners:
Spinner spinner; // Each one of the other spinners
String item; // Item selected in the current spinner
// Get the adapter for the other spinner
ArrayAdapter<CharSequence> array = spinner.getAdapter();
// Remove selected element in the current spinner from adapter
array.remove(item);
// Set adapter again
spinner.setAdapter(array);
Sorry but I didn't (and can't) test it...
In my application i have one spinner with data Cart,Trolley,Lorry,Truck etc..
in a button click i am saving spinner selected item and other items in the data base.
Now in another button click i want to display all saved data,so in that i want to display previously saved spinner item first instead of default one.
How can i achieve this,please anyone suggest me
Ex:in spinner 1,2,3,4 displayed now if i select 3 and saved in data base now this time i want to show spinner data as 3,4,1,2.
It's much simple by getting index of spinner from DB and set the currently selected index on spinner item,for example if the spinner position stored in DB
then set it as
spinner.setSelection(2);// Note : Position starts from 0,1,2,3 on array
You can change spinner content as below
String[] items = new String[] {"3","4", "1","2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
spinner2.setAdapter(adapter);
So I understand you correctly, you want to reorder the spinner items based on the user's previous selection? You just have to update the Adapter that you assigned to the spinner in that case.
I guess you wired up a simple ArrayAdapter in this case, so a basic solution would be to modify the order of the strings contained in that adapter, after selection.
ArrayList listArray = new ArrayList();
listArray.add("one");
listArray.add("two");
listArray.add("three");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listArray.toArray(strArray););
spinner2.setAdapter(adapter);
here took items as ArrayList and when user click on any item , break that arraylist in two part start to that point and point to last. then take one temp arraylist and add second part then first part so in that one .
and again call
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listArray.toArray(strArray););
spinner2.setAdapter(adapter);
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?