I have an issue which I just cannot get my head around. In my experience, code follows the sequence it is run in, but this does not appear to be the case in this instance.
I have two UI items, namely a spinner and an edittext field which are populated from a DB table in a function called from onCreate(). When the spinner is set (using setSelection) it kicks off the onItemSelected which sets the edittext value to a default value. After this, I then set the edittext field (using setText) to the value stored in the DB, but the new value does not appear.
Here is a snippet of the code:
spinnerdresslength.setSelection(backuplengthoptionid);
edittextskirtshortestlength.setText(backupshortestlength);
The event handler looks like as follows (I have simplified it for the sake of ease):
spinnerdresslength.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
edittextskirtshortestlength.setText("43");
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});`
Now despite the edittextskirtshortestlength.setText(backupshortestlength) following the spinner setSelection, the edittext shows 43 instead of backupshortestlength.
Can you anybody assist with this as I am at my wits end?
Thanks,
NicVegas
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) {
}
});
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 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.
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
this may be a simple question but i was wondering, i have an application that has two spinners and a textview which displays a boolean. the two spinners have numbers between 1 and 10 and i want the boolean to display true when ever the numbers from the two spinners equal to 12. i have all the code to do this and to check are the numbers in the spinners = 12 but i dont know where to put the code to check are the numbers = 12 when ever one of the spinners are changed
so long story short, is there a onClick command i can use to call this code when ever someone clicks on a spinner and changes the number in the spinner?
Thanks
Get an instance of the spinner and add a listener for when an item is selected. That way it will trigger after the user has clicked the spinner, and selected a value.
Spinner s = (Spinner)findViewById(R.id.myspinner);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
You have to use an on item selected listener:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// Here you check the spinner values sum
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
onItemSelected()
http://developer.android.com/resources/tutorials/views/hello-spinner.html