Spinner return null value - android

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) {
}
});

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)
{
}
});

Unable to get Spinner value when click item

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) {
}
});

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.

Android: two Spinner onItemSelected()

I have two spinners (day_spin and time_spin) in one Activity. I want to save the selected day_spin value into a variable. Is it possible to differenciate between the two spinners in the same Listener? Or do I have to write my own Listener class for each spinner?
Also I want to get the number of the selected item. For example I have String Array with 6 elements. I select number 3. How can I get the id?
day_spin = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter_day = ArrayAdapter
.createFromResource(this, R.array.spinner_day_array,
android.R.layout.simple_spinner_item);
adapter_day
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
day_spin.setAdapter(adapter_day);
day_spin.setOnItemSelectedListener(this);
time_spin = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter_time = ArrayAdapter
.createFromResource(this, R.array.spinner_time_array,
android.R.layout.simple_spinner_item);
adapter_time
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
time_spin.setAdapter(adapter_time);
time_spin.setOnItemSelectedListener(this);
This is my Listener:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
DAY = parent.getItemAtPosition(pos).toString();
TIME = parent.getItemAtPosition(pos).toString();
}
I hope you can help me!
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(parent.getId() == R.id.spinner1)
{
//do this
}
else if(parent.getId() == R.id.spinner2)
{
//do this
}
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
//parent.getItemAtPosition(pos);
//Spinner spnir = (Spinner)parent;
parent.getItemAtPosition(pos);
TextView tvReadValue = (TextView) findViewById(R.id.tvReadValue);
switch (parent.getId())
{
case R.id.spinDimension:
tvReadValue.setText("spinDimension" + pos);
break;
case R.id.speedDimension:
tvReadValue.setText("speedDimension" + pos);
break;
}
in onItemSelected method
int i = spinnerFirst.getSelectedItemPosition();
int j = spinnerSecond.getSelectedItemPosition();
String strName1 = yourFirstarray[i];
String strName2 = yourSecondarray[j];
try this...
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(parent.getId() == R.id.spinnerone)
{
// operations
}
else if(parent.getId() == R.id.spinnertwo)
{
//operations
}
}
Two Spinner in same class
I try a lot of this but at the last, I got a working Code.
state = findViewById(R.id.spinnerState);
country = findViewById(R.id.spinnerCountry);
List<String> categories = new ArrayList<String>();
categories.add("Select State");
categories.add("Andhra Pradesh");
categories.add("Arunachal Pradesh");
categories.add("Assam");
categories.add("Bihar");
categories.add("Chandigarh");
categories.add("Delhi");
categories.add("Goa");
categories.add("Gujarat");
categories.add("Haryana");
categories.add("Himachal Pradesh");
categories.add("Jammu and Kashmir union territory");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SaveUser.this, android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(dataAdapter);
state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), item, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// can leave this empty
}
});
List<String> categories2 = new ArrayList<String>();
categories2.add("Select Country");
categories2.add("Afghanistan");
categories2.add("Albania");
categories2.add("Algeria");
categories2.add("India");
categories2.add("Andorra");
categories2.add("Angola");
categories2.add("Antigua and Barbuda");
categories2.add("Argentina");
categories2.add("Armenia");
// Div Spinner implementing onItemSelectedListener
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(SaveUser.this, android.R.layout.simple_spinner_item, categories2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
country.setAdapter(dataAdapter2);
country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), item, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// can leave this empty
}
});
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (adapterView.getId() == R.id.number_of_poket_spinner) {
NumberOfPokets item = (NumberOfPokets) adapterView.getSelectedItem();
Toast.makeText(this, item.getPokets(), Toast.LENGTH_LONG).show();
} else if (adapterView.getId() == R.id.select_pleats) {
PleatsModel item2 = (PleatsModel) adapterView.getSelectedItem();
Toast.makeText(this, item2.getPleats(), Toast.LENGTH_LONG).show();
}
}

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