Display default value when I nothing selected from spinner array android - android

I make a spinner put the value in spinner using array,now even I was not select any value from spinner it automatically takes first value from spinner array, I want to set default value when nothing is selected form spinner array by user,means I get default value which I set ,when I was not selected any value, is this possible, and what is the use of onNothingSelected(AdapterView ....below code should run when user select manually any value from spinner but it run always and get first value which is in array,so please tell how to get default value when I nothing selected from spinner, can I use on nothing selected method..?
ArrayAdapter<String> CurrencyAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, Currency);
currency.setAdapter(CurrencyAdapter);
currency.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});

Better set first value of array,the value what you want to set as default. And then you can check whether that value is selected or something else to figure out,is spinner selected or not??

A related question I asked a couple of days ago could help you here, have a look at How to use Android Spinner like a drop-down list and see some of the answers which I found helpful.
And no, onNothingSelected() does not do what you're after... you want to have a look at the methods setSelection() and setSelectionInt() in the question I referred to.

Related

Spinner onItemSelected with item already selected

I've a Spinner with onItemSelected interation that works, but how the Api specification says:
This callback is invoked only when the newly selected position is
different from the previously selected position or if there was no
selected item.
I need to remove this limitation and i want that the callback is invoked also if the user select the same element.
How to do that? I read a suggestion about extending Spinner class and set the position to INVALID_POSITION, but i've not understood/able to do that.
Anyone did the same thing?
I also needed a solution to this problem. What I wanted to do was have a Spinner that has date ranges with a custom range option. The rows would look something like this:
Apr 10 - May 10
Mar 10 - Apr 10
Feb 10 - Mar 10
Custom Range
The problem is that if the user selects a custom range and then wants to change their custom range, they have to select a different range and then select the custom range option again. I wanted the user to just be able to select "Custom Range" again so that the custom range dialog could be shown again.
I sub-classed Spinner and created my own listener. The code switches the selection, but then immediately switches it so that nothing is selected. In my listener I just ignore any position that is less than zero.
The Spinner just displays the last selected item. I created my own custom adapter and specify what to display for each view, but that shouldn't be necessary. Here is how I sub-classed Spinner.
package com.example.widget;
import android.content.Context;
import android.widget.Spinner;
public class DateRangeSpinner extends Spinner {
private ItemSelectionListener listener;
public DateRangeSpinner(Context context) {
super(context);
}
/**
* This listener will be fired every time an item is selected,
* regardless of whether it has already been selected or not.
*
* #param l
*/
public void setOnItemSelectedListener(ItemSelectionListener l) {
listener = l;
}
public void removeOnItemSelectedListener() {
listener = null;
}
#Override
public void setSelection(int position) {
setSelection(position, true);
}
#Override
public void setSelection(int position, boolean animate) {
if (listener != null) {
listener.onItemSelected(position);
}
super.setSelection(position, animate);
super.setSelection(-1, animate);
}
public interface ItemSelectionListener {
public void onItemSelected(int position);
}
}
I hope this helps!
You can do this by a custom adapter, like create a layout of your desire views, then inflate this in custom adapter then on onItemClick function you can get the view by this function.
To distinguish each view you must to set the tag of each row.
It probably works in your condition.
Let me know any issue if you have
why you have select the selected item again. Just give a refresh button if you want to perform that task again.
This is the spinner Element with custom dialog mode and whitout promt:
<Spinner
android:id="#+id/spinner_metatag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/search_adv"
/>
The Array element where the default value is putted at position 0:
<string-array name="search_adv">
<item>#string/search_adv_prompt</item>
<item>#string/search_adv_title</item>
<item>#string/search_adv_desc</item>
<item>#string/search_adv_autore</item>
....
</string-array>
The String elements for the array with the default value:
<string name="search_adv_prompt">Scegli un metatag</string> <!-- Default value-->
<string name="search_adv_title">Titolo</string>
<string name="search_adv_desc">Descrizione</string>
<string name="search_adv_autore">Autore</string>
...
And here the code to prevent the event fired on onCreateMethod and the work around to permit to select the same element already selected:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_adv_main);
spinner = (Spinner) findViewById(R.id.spinner_metatag);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
//prevent onCreate event fire and the loop
if(pos==0)
return;
//HERE YOUR CODE
//at the end move to the default element the spinner
spinner.setSelection(0);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
Hope helps. The idea come from the second solution from TreKing's answer
Use OnItemClickListener instead of on itemSelectedListener.That will work for every click whether it is same or different.
I guess you must be storing the value in a variable, initialize the vairable with -1. And change the value as the user selects the item spinner, if the value is -1 ask the user to reselect or whatever u want.
#i want that the callback is invoked also if the user select the same element.
Android will do it for u as this is the default behavior of android.

Cannot use onItemSelectListener() with Spinner with One Item

So I have a spinner (spinner2 here) which is populated via an ArrayAdapter from an SQLite table. On selecting an item I want it
deleted from the DB
deleted from the spinner
The code below actually works. Except when the spinner has only one item. When that happens
it seems onItemSelected is not called at all.
I get the following LogCat
10-01 22:30:55.895: WARN/InputManagerService(1143): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#45a06028
Oh and when two items are populating the spinner, spinner.getcount() shows two items, so it's not some strange case of the system thinking the spinner is empty or something like that.
This is the code:
public class SpinnerItemSelectListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(parent == spinner2){
if(autoselected){
autoselected=false;
}
else{
//uniqvalarray is the arraymade from pulling data from SQLite and populaitng array adapter
Integer i = uniquevalarray.get(pos);
deleteRow(i);//deletes the row from the database and repopulates the above array.
autoselected=true;//just a boolean to stop autoslecting in onCreate()
//makeAlert(i);initially wanted to make alert box.
loadSpinner2();//reloads the spinner with new data
}
}
}
public void onNothingSelected(AdapterView parent) {
//TODO
}
}
The spinner runs this way : Only fire when you change the selected item . If you dont change that element , cause its the only one that exist , it can't change .
The solution i think you must use is using a button next to the spinner to throw the delete funcions.
You must think that Spinner is not made to be with an unique element , cause only changes usually when you change the selected one . then the natural solution can be that .

How to change data on one spinner by onclick on another spinner in android?

all i have two spinners spinner1 & spinner2. in one spinner i am showing names from database.(Ex. onkar) now if i select onkar name on spinner1 then all names must be occurred on spinner2 except name onkar.
i want to do like this.but i am not getting how to achieve it?
give me some sample or give idea for the same.
or can i change data of spinner dynamically????
Thanks in Advance--
consider that u have used two adapters to set data to spinner when u select first spinner just remove that value from second adapter and then call method notifyDataSetChanged() it will change second spinner data.
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
super.onItemSelected(parent, view, position, id);
// Step 1: Remove Data from ArrayList 1
// Step 2: Adpater for first spinner .notifiyDataSetChanged()
}

Android Spinner OnItemSelectedListener not working accurately

I have created a Spinner and activated the listener:
customerListSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> adapter, View view, int position, long id) {
// I do some work here
}
public void onNothingSelected(AdapterView<?> arg0) {
isInitRadioGroup = false;
}
);
I have added three values in the Spinner initially: "Search", "Employee", "Company".
If the user selects "Search", a new ListActivity is shown and the selected value is added to the above Spinner as a result.
Like this, I am performing some action on the selection of the spinner Item. Now, When the screen is shown at first time, by default "Search" is shown. To trigger the processing of the ListActivity, the user has to select the "Search" again. This time, on ItemSelected callback is not called. I mean to say, if a value in Spinner is already selected, then on the selection of the same value again, does not trigger the listener.
Whereas:
When the screen is shown at first time, by default "Search" is shown. The user selects the value "Employee" (processing for this is done, onItemSelected is called as the previous selected value is different than the value selected by the user). The user then selects the value "Search". This time the onItemSelected method is called and successfully adds a new value to the Spinner.
This is a bug of Android. I have checked there is no error in my code.
Try to use the spinner as following
Spinner genspin=(Spinner)findViewById(R.id.gender);
ArrayAdapter<String> genadap=new ArrayAdapter(this,R.layout.spinneritems,String_array);
genadap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
genspin.setAdapter(genadap);
genspin.setOnItemSelectedListener(this);
then you can get the selected value as
genspin.getSelectedItem().toString()

Android: Spinner with changing ArrayAdapter - how to identify?

I want to have three Spinners with contents which depend on each other.
E.g. Spinner1 displays {item1, item2} and Spinner2 either {item3, item4} or {item5, item6} depending on whether item1 or item2 is selected on Spinner1.
The same I want for Spinner 3, which reacts to changes of Spinner1 and/or Spinner2.
For the latter, I have to determine first which of the possible value sets is shown atm in Spinner2.
My question is kind of similar to this question, but I don't know what to do after getting the adapter.
That's what I have so far:
ArrayAdapter adapter1 = (ArrayAdapter) spinner2.getAdapter();
if(items_spinner1[0].contentEquals(adapter1.getItem(0)))
{
//...
}
I get the adapter, ask for the first value and compare it to the first String value of my Array to identify it. It doesn't at all seem elegant to me. Is there an easier solution?
You say the contents of the later spinners depend on the selection in the earlier ones, but the code you posted depends only on the contents of a spinner.
adapter1.getItem(0) returns the first item in the list, not the currently selected item. To get the currently selected item, use the spinner's (not the adapter's) getSelectedItem() method.
You could, for example, put something like this in your first Spinner's onItemSelectedListener (edited based on your comment below):
public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
Object selectedItem = parent.getSelectedItem();
// Do this if the first Spinner has a set of options that are
// known in advance.
if (/*selectedItem is something*/) {
// Set up the second Spinner in some way
} else if (/*selectedItem is something else*/) {
// Set up the second Spinner in another way
}
// OR, if you need to do something more complex
// that would cause too much clutter here, do this.
fillSecondSpinner(selectedItem);
}
Then place something similar in the second Spinner's onItemSelectedListener. Get the selected items from the first and second Spinners using getSelectedItem() (or the item positions using getSelectedItemId() for the first and the position parameter for the second). Use the selected items to set up the third.
Edit: The OnItemSelectedListener for the second Spinner would look something like this.
// This must be defined in the enclosing scope.
final Spinner firstSpinner; // Must be final to be accessible from inner class.
Spinner secondSpinner;
// ...
secondSpinner.setOnItemSelectedListener(new OnItemSelectedListener {
public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
// Again, usually the selected items should be of
// a more specific type than Object.
Object firstSelection = firstSpinner.getSelectedItem();
Object secondSelection = parent.getSelectedItem();
fillThirdSpinner(firstSelection, secondSelection);
}
public void onNothingSelected (AdapterView<?> parent) { }
});

Categories

Resources