How to change the content of Spinners dynamically - android

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

Related

How to get the index of string-array

In strings.xml I have string-array which populates the spinner.
I have an note app where user can select subject from spinner and show in recycler view.
So after user added note and if the user clicks the note then it should redirect to that spinner and auto select the subject which is selected previously.
I tried to set position as sub_spinner.setSelection(subPosition) but I can't figure out how to get the subPosition
String.xml
<string-array name="subject">
<item>Communication English</item>
<item>Computer Graphics</item>
<item>COA</item>
<item>Data Communication</item>
<item>Instrumentation II</item>
<item>Software Engineerinng</item>
</string-array>
So if I have sub as COA then i should get index of 2.
Since you have the subject string-array in the xml. You can get the string array in your java code by using
List<String> list= Arrays.asList(getResources().getStringArray(R.array.subject));
Then in order to get the index of the selected subject, you can can implement an OnItemSelectedListener on the Spinner or ListView; anything that you have populated your string array on.
The OnItemSelectedListener implementation has a callback like onItemSelected with parameters, you want the position which provides the position of the selected item. Then you can do:
setSelection(position)
inside of the OnItemSelectedListener. If you need further guidance on how to implement, you can post some more code and I will provide you the exact code to do this.

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

How to change the entry of a spinner in code (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.

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

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