I have problem with android. I'm adding data from adapter to spinner, then I set the selection on item on position 0, after that I create OnItemSelectedListener and it is triggered but I don't know why...
onCreate method:
this.spinner = (Spinner)findViewById(R.id.spinner);
this.spinner.setAdapter(MyAdapter.GetAdapter());
this.spinner.setSelection(0);
this.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do some things
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Maybe here is something wrong, I also try to remove the listener, change the selection and then add listener to it. Maybe I do sth wrong here:
AdapterView.OnItemSelectedListener listener = this.spinner.getOnItemSelectedListener();
this.spinner.setOnItemSelectedListener(null);
this.spinner.setSelection(((SpinnerAdapter) this.spinner.getAdapter()).getElementIndexByTitle(array.get("ProductName")));
listener.onItemSelected(null, null, -1, -1);
this.spinner.setOnItemSelectedListener(listener);
It seems that it triggers the event also after: this.spinner.setOnItemSelectedListener(listener); Maybe is there possibility to select item without triggering OnItemSelected ?
I found out that inside the AdapterView there is a SelectionNotifier and it wokrs automatically. So I need to remember last selection index and then compare it with position in OnItemSelected.
Related
I want to develop an Android app that should run on devices with an external keyboard. The user should be able to go through a form using the enter key. Now, I have an issue with a Spinner and a Button.
The only way (I found) to transfer the focus from the Spinner to the Button is to use an setOnItemSelectedListener:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
root.findViewById(R.id.button).requestFocus();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {}
});
This works when the user selects another item from the Spinner than the current value. However, it doesn't work when the user opens the dialog (with entering), then doesn't select another value (but presses enter to confirm the current choice). I guess the OnItemSelected-Event isn't triggered when the value doesn't change.
Does anyone have a clue how I could implement that?
As the onItemSelected in the spinner is not called if you don't change the selection then you would have to do something like this
Force the user to select something from the spinner by adding a default value to the spinner like "Please select". Thus you could also show some error message if he doesn't select and eventually your onItemSelected listener would get invoked.
I ended up by using the solution from this answer: (Which extends the Spinner-class)
How can I get an event in Android Spinner when the current selected item is selected again?
Then, it is possible to shift the focus to the next item with
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
root.findViewById(R.id.Button).requestFocus();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
I have two Spinners in an activity, aSpinner and bSpinner. The contents of the second spinner are dependent upon the selection of the first spinner. Currently, the onItemSelected method sets the adapter for the second spinner based on the first spinner.
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (parent == aSpinner) {
buildAdapterForSpinner(bSpinner, getSpinnerOptions(pos));
// Do something
} else if (parent == bSpinner) {
// Do something
}
}
I want to programmatically select options on both of these spinners.
#Override
public void onCreate(Bundle savedInstanceState) {
...
aSpinner.setSelection(1, true);
bSpinner.setSelection(1, true);
}
The first selection works just fine, but the second selection has no effect. However, if I introduce a 50ms delay before the second selection, everything behaves the way I want it to.
#Override
public void onCreate(Bundle savedInstanceState) {
...
aSpinner.setSelection(1, true);
new Handler().postDelayed(() -> bSpinner.setSelection(1, true), 50);
}
My suspicion is that onItemSelected is not being called immediately after the first spinner selection, therefore the second spinner has no adapter and cannot make a selection.
Using a hardcoded delay like this seems hacky. Is there a better way for me to make both of these selections without having to use a delay?
activity_assessment.xml
assessment.java
assessment.java
arrays.xml
Eventhough , i am clicking on submit button i am unable to move to another activity.
Can you please help me in this how to navigate to other new activity by using two spinners and a submit button?
Above i attached my source code files
DON'T USE == in the if statement if(SpinnerValue1)
USE
.equals("TEST-1")
Like:
if(SpinnerValue1.equals("TEST-1"))
because SpinnerValue is an Object and you can't logically compare objects with "=="
See this
Also, I have this simple way of working with spinners :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Instead of using getSelected(), use position
spinnerValue = position;
//or
useValueFrom(yourDataArrayAt[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I have problem in listening user interaction of the spinner when item selected.
I know the below listener work perfect
spinner.setOnItemSelectedListener(this);
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
but In my application scenario I will change the spinner value through program,that time also it fire the onitemselected event.but I need this event should fired only when user interaction happened on the spinner
Note:I am not talking on initial spinner value selected item.
Please Let me know how can I accomplish this
Thanks in advance,
Naveenkumar.R
tempListener = spinner.getOnItemSelectedListener();
spinner.setOnItemSelectedListener(null);
//change the spinner value...
spinner.setOnItemSelectedListener(tempListener)
More elegantly, you could subclass the Spinner class, and create 'disableOnItemSelectedListener()' and 'enableOnItemSelectedListener()' methods, which do the same as the code above.
Undesired calls to onItemSelected can be avoided using a simple technique I describe in the following link. This way, you can yourself setSelection anytime without a worry. Check the accepted answer to this question:
Undesired onItemSelected calls
The java.lang.RuntimeException is "Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead," but that is not correct. I am using setOnItemClickListener to do some stuff based on the new selection, but I also need to do some stuff before the user changes the selection. Specifically, I am collecting data for each selection that needs to be saved to a file before moving to another selection, since the other selection is associated with different set of data. Is there a way to use setOnClickListener with an Android Spinner?
spinner.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do some stuff before the user changes the selection
...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
// Do some stuff based onItemSelected
...
You can replicate the an onclick event using ontouch events
this.spinner=(Spinner)findViewById(R.id.spinner);
this.spinner.setClickable(false);
this.spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG, "spinner touch");
//replicating a click
if(event.getAction() == MotionEvent.ACTION_UP){
v.playSoundEffect(android.view.SoundEffectConstants.CLICK);
}
return true;
}
});
You will have to set the Click listener on the underlying view (normally a TextView with id: android.R.id.text1) of the spinner. To do so:
Create a custom Spinner
In the constructor (with attributes) create the spinner by supplying the layout android.R.layout.simple_spinner_item
Do a findViewById(android.R.id.text1) to get the TextView
Now set the onClickListener to the TextView