Unable to get Spinner value when click item - android

I am here trying display seleted value from dropdown spinner, but mysetOnItemSelectedListener is not working
My code is as follows:-
ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.array.location,list);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(
parent.getContext(),
"The color is "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});

ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.location,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(
parent.getContext(),
"The color is "
+ spinner.getSelectedItem().toString(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Make necessary changes and try the above code.If it not works then change Charsequence to String.

try this code
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Employee employee = employees.get(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Related

Spinner.setOnItemClickListener not working

This is my code :
transactionTypeSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Something changed", LENGTH_SHORT).show();
}
});
But unfortunately OnItemClickListener method not working and show this message : setOnItemClickListener cannot be used with a spinner.
How can I solve the problem?
Thanks!
You should not call OnItemClickListener for a spinner. A Spinner does not support item click events.
transactionTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(MainActivity.this, "Something changed", LENGTH_SHORT).show();
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
You need to use setOnItemSelectedListener()
Register a callback to be invoked when an item in this AdapterView has been selected.
Use
transactionTypeSpinner.setOnItemSelectedListener()
instead of
transactionTypeSpinner.setOnItemClickListener()
SAMPLE CODE
transactionTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Something changed", LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Please try with the following code hope will help you
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("Add new category"))
{
// do your stuff
}
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});

Spinner return null value

when i am selected spinner value then some time it return a true value and some time it return null value
My spinner code is
list.add("Please Select Approver Name");
list.add("bishnu");
list.add("bishnu");
list.add("bishnu");
list.add("bishnu");
ArrayAdapter<String> adp1=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,list);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adp1);
sp1.setSelection(0);
Item Selected Listener
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
strspin = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Toast.makeText(context, "value"+strspin, Toast.LENGTH_LONG).show();
I am working on it past two day
Please Help Me how i can fix this problem
Use the Toast inside the onITemSelected method as below.
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
strspin = parent.getItemAtPosition(pos).toString();
Toast.makeText(context, "value"+strspin, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});

onclick on a spinner?

Hy,i need to have an onclick on a spinner.
This is my spinner:
Spinner spinner = (Spinner) findViewById(R.id.spnTarghe);
final String targhe[]=risposta.split("/");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_item,
targhe
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
How can I do for use an onclicklistener that do this simple istruction:
sTarga=targhe[position];
By using onitemselected listener you can select item in spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String mselection=spinner.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), "selected "+ mselection, 30).show();
/**** do your code*****/
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
//
}
});
use onItemSelected
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
sTarga=targhe[position];
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

Android Spinner Item selected text

It looks pretty straight forward but it's not working for me. I am trying to get item text from a Spinner when Item is selected.
sp = (Spinner) findViewById(R.id.stnacspinner);
cspinner = aircraft.getDistinctStation();
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Functions.CustomToastShort(getBaseContext(), sp.getSelectedItem().toString() );
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
It's returning "android.database.sqlite.Sq;LiteCursor#40e06dc0"
What am I doing wrong ?
well you are getting the string representation of the object which is memory address.
sp = (Spinner) findViewById(R.id.stnacspinner);
cspinner = aircraft.getDistinctStation();
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Functions.CustomToastShort(getBaseContext(), sp.getSelectedItem().toString() );
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
sp.getSelectedItem().toString() //returns "android.database.sqlite.Sq;LiteCursor#40e06dc0"
selectedItemView.getText() or sp.getSelectedItem().getText() should do the trick.

Create Spinner Programmatically

I have to create a Spinner which is not in my Layout.
so I want to create it Programmatic I write the following code in My Activity class
Spinner spinner = new Spinner(this);
String option[]={"By Date","By Transaction ID","By Customer Phone"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,option);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter(spinnerArrayAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
Log.d("in select","yes");
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast.makeText(MainMenu.this,"no",Toast.LENGTH_LONG).show();
Log.d("In No select","No");
}
});
so My question is how I can access the spinner selected item in the activity
Your only way to access it outside your listener is making a variable that will hold the position, so you would use it later.
Something like this:
public class YourActivity extends Activity {
private int curPos;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spinner spinner = new Spinner(this);
String option[]={"By Date","By Transaction ID","By Customer Phone"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,option);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter(spinnerArrayAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
curPos = position;
Log.d("in select","yes");
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast.makeText(MainMenu.this,"no",Toast.LENGTH_LONG).show();
Log.d("In No select","No");
}
});
}
In your onItemSelected:
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
Log.d("in select","yes");
}
The position helps you to retrieve your selected item. Which, in your case, it should be:
option[position]

Categories

Resources