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.
Related
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 :)
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) );
When normally populating a Spinner as I have done in the past I normally use a SpinnerAdapter then normally have items in resources to populate it.
I have currently though a different query, I have in my code a user input for an int and I want my spinner to populate with numbers up to the user selected number. So if the user enters the number '5' it is saved to an int variable. I then want the Spinner to show 1,2,3,4,5 as choices.
I am really not sure how I would approach this.
Thanks, Oli
Edited
Below is a basic example of how you would add Integers to your spinner :
mspin=(Spinner) findViewById(R.id.spinner1);
Integer[] items = new Integer[]{1,2,3,4};
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, items);
mspin.setAdapter(adapter);
You can refer to this and make changes in your project as per your logic. Also in your case you should use an ArrayList of integers since the number of choice of the user seems to be dynamic. you can create an arraylist and replace in for the Integer array in the above code.
Hope this helps!!
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);
}
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...