How to change the entry of a spinner in code (Android) - android

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.

Related

Retrieve position of item within Spinner made up from String.xml array

I am working on an android project and have a spinner which contains items from a string-array which is in the string.xml file.
In the strings.xml I have the following array
<string-array name="array_loginType">
<item>Select Login Type</item>
<item>Website</item>
<item>App</item>
<item>Other</item>
</string-array>
and the spinner contains the following XML
<Spinner android:id="#+id/add_cboLoginType"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:prompt="#string/add_select_login_type"
android:padding="4dp"
android:entries="#array/array_loginType"/>
At some point the user can select the item from the spinner and when submitted it saves the item in the database. I am then allowing the user to edit the details and I am trying to set the selected item within the spinner based on the item that was retrieved from the database. I.e. if the saved item within the database says Website then Website will be selected inside the spinner.
Thanks for any help you can provide.
If you know what position in the array holds the correct selection, you can just use the Spinner.setSelection();-method to set the spinner to display it.
In your example, the Website is found in position 1 of the array (1st actual entry is number 0).
Therefore, your code should look something like this:
// Declare the spinner object
Spinner mySpinner = (Spinner) findViewById(R.id.add_cboLoginType);
// Set the correct selection
mySpinner.setSelection(1, true);
The second argument tells the spinner to "animate" the selection - so it actually displays the correct selection, and not just sets the correct value (if it's set to false or not included at all, the spinner will have changed (so anything depending on the selection will work as intended) but it'll still appear to be at the default selection).
So you want to have the user select a type and save it with some other data in a database and when the user tries to edit that data you want to edit screen to have a preselected spinner, correct?
First you need a OnItemClickListener. This will let you know when the user selects something:
Spinner spin = (Spinner) findViewById(R.id.add_cboLoginType);
spin.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, final View view,
final int position, final long id) {
// update the type field on the data object you are creating or editing
// position is the type index
obj.setTypeIndex(position);
}
}
);
That is how you see the change, now to pre-select is in edit mode:
//editMode boolean.. why not
if (editMode) {
spin.setSelection(obj.getTypeIndex, true);
}

can we set pre text in the spinner in android

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

How to show Spinner selected Item first in Android

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

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?

Setting the position within a spinner

Good Afternoon,
I have a spinner array containing 3 spinners. Each spinner is populated via a res string array. Each array contains the numbers 1-100. When the activity starts each spinner contains a string array of 1-100 and when you click on a spinner the first choice is 1.
Say a user picks 25 on the first spinner. I'd like the 2nd spinner to show 25 as the starting point for scrolling when the spinner is clicked but not fire the spinner. The 2nd spinner would still contain the array 1-100 though so a user could scroll down to a lessor number if the wanted to.
I've tried using setSelection but that causes the 2nd spinner to fire causing undesirable effects (an edit box is populated with the 2nd number even though the user hasn't clicked the 2nd spinner). I would like the 2nd spinner to just show 25 as the starting point.
How do I do this?
setSelection() is the only way I know, it shouldn't be the cause for the issue.
In my case I have:
Spinner spinner = new Spinner(getApplicationContext());
spinner = (Spinner) findViewById(R.id.spinner);
//search for the position that we need to move to,
//the spinner has an array adapter set to it
int recordInPreferences = WidgetProvider.settings.getInt("SpinnerChoice", 0);
int counter = -1;
do {
counter++;
} while (
counter < getResources().getIntArray(R.array.stringofvalues).length &&
getResources().getIntArray(R.array.stringofvalues)[counter] != recordInPreferences
);
spinner.setSelection(counter, true);
I do this consequentially for several spinners one after the other and it works well.

Categories

Resources