I used setOnItemSelectedListener to specify which item is selected in a spinner, but i think it is not called. it should print "It works" when the program runs.
XML code:
<Spinner
android:id="#+id/quantity"
android:layout_width="94dp"
android:layout_height="27dp"
android:layout_x="11dp"
android:layout_y="118dp"
/>
Java code:
public class quantity extends Activity
{
Spinner quantity;
public void onCreate(Bundle savedInstanceState)
{
quantity=(Spinner)findViewById(R.id.quantity);
quantity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
Object item = parent.getItemAtPosition(pos);
System.out.println("it works... ");
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
}
Try this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout);
//to fill your Spinner
List<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("Item 1");
spinnerArray.add("Item 2");
spinnerArray.add("Item 3");
spinnerArray.add("Item 4");
spinnerArray.add("Item 5");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner) findViewById(R.id.quantity);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
Object item = adapterView.getItemAtPosition(position);
if (item != null) {
Toast.makeText(MainActivity.this, item.toString(),
Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this, "Selected",
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// TODO Auto-generated method stub
}
});
}
you have missed setContentView(R.layout.yourlayout);
Related
I have two spinners in my code. The problem I face is I'm unable to differentiate the results of the two spinners.I know we can use ids but I'm unable to do it.here's my code
public class AddProduct extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_product);
Spinner spinner = findViewById(R.id.spinner1);
SpinnerAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner, new String[]{"Men", "Women", "Kids"});
spinner.setAdapter(adap);
spinner.setTag(1);
spinner.setOnItemSelectedListener(this);
Spinner spinner2 = findViewById(R.id.spinner2);
spinner2.setTag(2);
SpinnerAdapter adap2 = new ArrayAdapter<String>(this,
R.layout.spinner, new String[]{"Casual", "Formals", "Ethinic"});
spinner2.setAdapter(adap2);
spinner2.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text , Toast.LENGTH_SHORT).show();
}
Modify onItemSelected method
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = "";
if(parent.getId() == R.id.spinner1) {
text = "spinner1 " + parent.getItemAtPosition(position).toString();
} else if(parent.getId() == R.id.spinner2) {
text = "spinner2 " + parent.getItemAtPosition(position).toString();
}
Toast.makeText(parent.getContext(), text , Toast.LENGTH_SHORT).show();
}
differentiate the results:
Spinner s1, s2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_product);
s1= findViewById(R.id.spinner1);
SpinnerAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner, new String[]{"Men", "Women", "Kids"});
s1.setAdapter(adap);
s1.setTag(1);
s2= findViewById(R.id.spinner2);
s2.setTag(2);
SpinnerAdapter adap2 = new ArrayAdapter<String>(this,
R.layout.spinner, new String[]{"Casual", "Formals", "Ethinic"});
s2.setAdapter(adap2);
s1.setOnItemSelectedListener(this);
s2.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.spinner1)
{
String sp1= String.valueOf(s1.getSelectedItem());
// Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if(sp1.contentEquals("Men")) {
//Do Something
}
if(sp1.contentEquals("Women")) {
//Do Something
}
if(sp1.contentEquals("Kids")) {
//Do Something
}
} else if(spinner.getId() == R.id.spinner2)
{
String sp2= String.valueOf(s2.getSelectedItem());
// Toast.makeText(this, sp2, Toast.LENGTH_SHORT).show();
if(sp2.contentEquals("Casual")) {
//Do Something
}
if(sp2.contentEquals("Formals")) {
//Do Something
}
if(sp2.contentEquals("Ethinic")) {
//Do Something
}
}
}
i want to take the value in spinner and user press the ok Button and then start new activity from against the value of spinner. The string array is just like that.
public class Menu extends Activity implements View.OnClickListener {
private String[] array_spinner = {"Select Here", "Honda", "Toyota", "Mitibushi"};
private String[] array_spinner01 = {"Select Here", "Civic", "Gli", "Lancer"};
// //private String[] array_spinner02={"1999","2000","2005"};
// Spinner s,s1,s2;
// Button ok;
//#Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
Spinner s, s1;
Button ok;
Object _globalString;
Object _globalString2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
s = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, array_spinner);
s.setAdapter(adapter);
s1 = (Spinner) findViewById(R.id.spinner01);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, array_spinner01);
s1.setAdapter(adapter1);
//
// s2=(Spinner)findViewById(R.id.spinner02);
// ArrayAdapter<String> adapter2=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,array_spinner02);
// s2.setAdapter(adapter2);
ok = (Button) findViewById(R.id.btn_ok);
ok.setOnClickListener(this);
s.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
_globalString = parent.getItemAtPosition(position);
}
//#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
s1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
_globalString2 = parent.getItemAtPosition(position);
}
// #Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn_ok) {
if (_globalString.equals("Honda") || _globalString2.equals("Civic")) {
startActivity(new Intent(getApplicationContext(), Civic.class));
} else if (_globalString.equals("Toyota") || _globalString2.equals("Gli")) {
startActivity(new Intent(getApplicationContext(), Toyota.class));
}
}
}
}
please tell me where is my mistake in this code because i stucked and i cant find it
There are many ways to do this, but I would recommend to follow these solution :
First :
save the value of the spinner in a global string variable by declaring a string at the top of your Activity like below :
public class Menu extends Activity implements View.OnClickListener {
Spinner s1, s2;
Button acceptbutton;
String _globalString ;
String _globalString2 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Your code ..
Now you need to save the value of your selected field into that string, everytime your spinner changes value :
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
_globalString = parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
s.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
_globalString2 = parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Now you can switch activity based on spinner value :
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn_ok) {
if (_globalString.equals("Honda") || _globalString2.equals("Civic")) {
startActivity(new Intent(getApplicationContext(), Civic.class));
} else if (_globalString.equals("Toyota") || _globalString2.equals("Gli")) {
startActivity(new Intent(getApplicationContext(), Toyota.class));
}
}
}
I have a spinner with ten numbers 0-10 and I if someone picks a number I want him not to be able to pick the same value again. So inside ItemSelected I do the following with no result
#Override
public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
// On selecting a spinner item
if (position > 0 && position!=tempPosition)
{
TeamSpinnerNumber = adapter.getItemAtPosition(position).toString();
tempPosition = position
}
}
Check this i worked out for you may it help you.
public class MainActivity extends AppCompatActivity {
ArrayList<String> mStrings;
ArrayAdapter<String> mStringArrayAdapter;
Spinner mSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = (Spinner) findViewById(R.id.spinner);
mStrings = new ArrayList<String>() {{
add("None");
add("one");
add("two");
add("three");
}};
mStringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
mSpinner.setAdapter(mStringArrayAdapter);
mSpinner.setSelection(0);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String string = parent.getItemAtPosition(position).toString();
if (!string.equals("None"))
removeThisFromSpinner(string);
mSpinner.setSelection(0);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void removeThisFromSpinner(String s) {
mStrings.remove(s);
mStringArrayAdapter.notifyDataSetChanged();
}
}
try this, hope it helps :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG, "onItemSelected: "+position);
//checking the position of the selected spinner item, if the previous position is not the same then do something here
if(position>0 && position!=myPosition){
}
myPosition = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
and don't forget to add global variable to the class :
int myPosition = 999; // the non existed position make it global variable
In my application, I use spinner, it shows with spinner.performClick(). After a lot of tries I noticed that my code does not trigger onItemSelected method, Then I tried to add #Override annotation but it returns as error and says delete that annotation. here is that code:
Spinner colorSpinner = new Spinner(this);
ColorFriendsSpinnerAdapter adapter = new ColorFriendsSpinnerAdapter(getApplicationContext(), liste, R.layout.color_dropdown, new String[] { "Icon" }, new int[] { R.id.colorDropdown });
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
colorSpinner.setAdapter(adapter);
colorSpinner.performClick();
colorSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
editColorXml(position);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Thanks for any help, and sorry for my English.
A sample of ItemSelectedListner , no need of override annotation .
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
set listener before colorSpinner.performClick() . in this code spinner will be clicked at the moment where no listener is set .
Try this Code..
final List<String> list=new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
list.add("Item 5");
final Spinner sp1= (Spinner) findViewById(R.id.spinner1);
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.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), list.get(position), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
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]