I am designing a mobile app for jewellery shop. In Android, for the jewellery product description page, I have a Spinner with some ring sizes with their text 5 to 25 set by using a custom array list adapter.
I have a Radio Group of 18 carat or 22 carat that work along with the Spinner, and if different Radio Button for carat is selected, then the spinner text should change along with it only for a few ring sizes, for example 17 - Ships in 24 hours if the ring size is ready to ship and 18 if the ring size is to be newly made based upon customer's order.
To achieve this, if a different size Radio Button is selected, I create a new array list, add all the new text again for spinner and then use array adapter again.
ArrayAdapter adapter = new ArrayAdapter(Description.this, android.R.layout.simple_spinner_item, sizes);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
In doing so, the Spinner automatically defaults to the first item 5 always, even if previously the spinner item selected was, for example, 17 size for a different carat Radio Button.
And also, the other details corresponding to other Text Views in the same activity, for example price, discount, making charge, gold weight, delivery date, whether product is ready to ship, etc. automatically default to the ring size 5.
So, is it possible to change the text of a specific Spinner item programmatically if a different carat Radio Button is selected? Is it possible to achieve that without creating a new array list, add all the new text to it and making use of array adapter again? And if so how?
If sizes is an ArrayList, to change a particular item in the spinner try using
sizes.set(index,newItem)
If you want to change the 3rd item to 10,
sizes.set(2,10) //I am assuming the sizes arraylist is integer arraylist. if not change to required data type
then execute
adapter.notifyDataSetChanged() // This will refresh you spinner.
instead of re-setting the adapter on the spinner. And then if you want, execute
s1.setSelection(2)
The s1.setAdapter(adapter); should only be executed once(when the spinner is initialised).
public void refreshSpinner(int index,int newValue){
sizes.set(index,newValue);
adapter.notifyDataSetChanged();
//Only if you want to select the new value
s1.setSelection(index);
}
Try below code,
Select the Spinner element at index position 2
(It will select third element/item from Spinner)
s1.setSelection(2);
Related
Have created n number of spinners pro grammatically. need to select spinner without clicking the spinner item.
it should select only when click the button.
(ex) 50 spinners have created using loop
it should select specific spinner based on button click.
You can select specific spinner item by,
spinner.setSelection(position);
hopes this solves your problem.
I am using a Spinner and ArrayAdapter to populate items in Spinner. I want to make the spinner multi-selectable i.e. user can select multiple items and should also be able to deselect to none. My code is as below,
spnAdapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_multichoice,items);
spnAdapter.setDropDownViewResource(android.R.layout.simple_list_item_multiple_choice);
spnProducts.setAdapter(spnAdapter);
Here the items is an array list.
This code gives me a spinner with check boxes but it does not allow multiple selection. What should I do to make it multiple selection?
Thanks in advance.
I want to hold numbers from 1 to 100 in a spinner drop down and limit the numbers of items visible to 3. All the other numbers should get by scrolling in the spinner dropdown.....please help
There is no default method in spinner to control number of items to display in drop-down. But You can do this by setting customized adapter to spinner.
I have a problem that I have two spinners, second spinner is dependent on first (means when I select a item from 1st Spinner then according to that Second Spinner gets filled). But the problem is, I want Select Keyword as a default Item for both spinners, and when I will click on that, spinner should filled with parsed Info and when I will select an Item from first Spinner then the Parsing starts for Second Spinner but Spinner remains with default Select Keyword, when I will click on second spinner then it should be filled by the parsed Info.
How can I implement this, because when I am setting the adapter in any spinners its onItemSelectedListner first called, which is not desirable.
You will need to add "Select" as your first item in your array list..
and on click event of the Spinner you just remove the First item from your array list
you will also find many similar question in Stack Overflow..search for it and you will get your exact answers with example.
You Can do this by Dialog, make a Customize dialog with List Box and fill the list with parsed data after click on TextBox which contains Select Keyword as a default.
I have an android spinner that's populated by a list of strings using an ArrayAdapter and it operates fine, however because of the way the spinner is displayed I'm running into a display height problem with the list items.
At first glance, it would seem that the ArrayAdapter can use a single layout for displaying options which leads to the problem I'm having. When displaying the current item in the spinner (when the user is not selecting a new item from the list) the spinner pads the text so that the spinner is a reasonable size for clicking on. However, when the user taps on it and brings up the list to select a new item, the list items presented are way too small height-wise. If I use an item layout that presents the list items at a reasonable height, then the spinner itself becomes exorbitantly huge due to its own padding of the list item.
Any ideas on how I can manage the height of these two item display modes so that effectively they display with the same height value instead of the spinner height being larger than the list item display height?
I've run into this issue myself a while ago, and it turned out that I need to use different layouts for dropdown and display
I have this code:
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cGroups,
new String[] {
"name", "_id"
}, new int[] {
android.R.id.text1
});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Yes, the above answer is correct.
It took me forever to find this, because it's wrong in the sdk samples for 2.2 Android. And I had a hard time accepting that.
Here's a snippet from
samples/android-12/Spinner/src/com/android/example/spinner/SpinnerActivity.java:
this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets,
android.R.layout.simple_spinner_dropdown_item);
while it should have android.R.layout.simple_spinner_item there instead and simple_spinner_dropdown_item should only be used for the dropdown items. Otherwise the spinner arrow get streched and it draws dropdown selection circle to the display, too.