This question already has answers here:
Spinner : onItemSelected not called when selected item remains the same
(11 answers)
Closed 5 years ago.
I have used Spinner to display toast on particular item selected in Spinner.
I am displaying toast when "other" is selected in Spinner.
I have done it as below :
spinnerTemp=(Spinner)findViewById(R.id.spinnerTemp);
spinnerTemp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(spinnerTemp.getSelectedItem().toString().equals("other")){
Toast.makeText(SocialLoginActivity.this, "Displayed", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
The issue is When I am selecting "other" for the first time, toast is displaying.. : FINE. Now, on the spot If i select "other" again, then toast is not displaying. Why ?
Now, if i select some other value and try again selecting "other", its working fine. Issue is with the selecting "other" one after another.
What might be the issue ?
Thanks.
Here a little better implementation:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
Try this
spinnerTemp=(Spinner)findViewById(R.id.spinnerTemp);
spinnerTemp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = spinnerTemp.getSelectedItem().toString();
if(item.equalsIgnoreCase("other")){
Toast.makeText(SocialLoginActivity.this, "Displayed", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Related
I am trying to add spinner selected position value and edittext into sqlite database but I don't know how would I use Spinner position value during insertion into sqlite database.
Someone help me please.
Below is the spinner Item position code:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select expense type")) {
// Do nothing
} else {
item = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
//Do nothing
}
Below is the Code where I want to use Spinner Item Position selected value and edittext value togather for inserting into sqlite database. so how could I get spinner position value for use into InsertExpense() function .
public void InsertExpense() {
addExpId.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public void onClick(View v) {
}
});
declare a global variable to store the selected item in spinner
String selectedItem=";
and here
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//yourList is the array your are using in your spinner adapter
selectedItem=yourList.get(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
//Do nothing
}
and then use this variable in your insertion method
i have two spinners, city and neighborhood, when i select a city it populates neighborhoods list. but i want when i click neighborhood spinner and city is empty to show a message or an Alertdialog.
i tried to use ontouchevent on the second spinner but it doesn't work.
i m using custom spinner com.toptoche.searchablespinnerlibrary.SearchableSpinner thanks for help
neighborhoodSpinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Toast.makeText(getApplicationContext(),"city emty",Toast.LENGTH_LONG).show();
}
return false;
}
});
why don't you use onClickListener instead?
s.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
cityspinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItemcity = parent.getItemAtPosition(position).toString();
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
neighborhoodSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItemneighbour = parent.getItemAtPosition(position).toString();
if(selectedItemcity.equals("your item name")) //Also you can check previous spinners position here if 0 then show popup or dilaog
{
// do your stuff
}
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
Use setOnItemSelectedListener to get spinner item selection listener.
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
// Write your code here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
It depends whether your spinner has a default value to mean nothing.
For example, if your spinner has a city value of "Please select a city" or something like that, which doesn't have to be the case.
Your spinner may already have a city selected when it's populated.
In the case that "Please select a city" is an option, this code might help:
Spinner city= findViewById(R.id.spinner_city);
Spinner neighborhood = findViewById(R.id.spinner_neighborhood);
neighborhood.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(city.getSelectedItem().toString().equals("Please select a city")){
Toast.makeText(MyActivity.this, "Please select a city first", Toast.LENGTH_SHORT).show();
}else{
// do nothing
}
}
});
Is there a way to get the item position on Spinner Object by only focusing and not by OnItemSelected in Android?
Cause i need to add/increment an item when everytime the last item of spinner is focus..
Try this.It is working fine.
#Override
public void onItemSelected(final AdapterView<?> parent, View view,
final int position, long id) {
parent.post(new Runnable() {
#Override
public void run() {
spinner.requestFocusFromTouch();
long pos = spinner.getItemIdAtPosition(position+1);
Toast.makeText(getApplicationContext(), "Position : " +pos, Toast.LENGTH_SHORT).show();
}
});
}
I've a spinner control that I connected it via a dataadapter and I get the data through it .
in the class, I want to get the selected spinner control's id .
How can I do that ?
I've used this code but when I run it , it says there's a problem and it closed .
Toast toast=Toast.makeText(MainActivity.this,sp.getSelectedItemId(),5000);
toast.setGravity(Gravity.CENTER,100, 0);
toast.show();
I tried getSelectedItemId and getSelectedItemPosition but non of them worked .
Here the answer for your question
ArrayAdapter<String> adpt = new ArrayAdapter<String>this,android.R.layout.simple_spinner_item, strType);
adpt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnType.setAdapter(adpt);
spnType.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
int intItemType = spnType.getSelectedItemPosition();
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
This the line getting selected position of the spinner
int intItemType = spnType.getSelectedItemPosition();
Try this
//Spinner OnItemClick Event here
payfeeTabStudentNameSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String spinnerSelectedValue = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Please let me know your problem is resolved or not.
How can i set the hash table?I've two variables,which are book name and number of chapter,inside spinner.I want to set text view according to book name and number of chapter.So how can i do it?
Basically, you have your elements inside an Arraylist.
All you need to do is fetch the element that is selected in this spinner.
spinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
//selectedItemView.invalidate()
Toast.makeText(Registration_Screen.this,
arrayList.get(position) + "",
Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
Toast.makeText(Registration_Screen.this,
"Nothing Selected", Toast.LENGTH_LONG).show();
spinner.setVisibility(View.GONE);
}
});
So now you have the position of the arraylist, where your item is present.
let mw know if this works.