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) {
}
});
Related
I have a fragment that creates controls at runtime, first create a spinner and then the second, the detail is in the second spinner depends on having the first selection.
As I can update the data of the second spinner depending on the selection of the first?
final Spinner Combo2 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
final Spinner Combo1 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
List<String> list = new ArrayList<String>();
list.add("TERRESTRE");
list.add("FLUVIAL");
list.add("AEREO");
final ArrayAdapter<String> adapterCombo = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
,android.R.layout.simple_spinner_item,list);
List<String> list2 = new ArrayList<String>();
list2.add("OP1");
list2.add("OP2");
final ArrayAdapter<String> adapterCombo2 = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
,android.R.layout.simple_spinner_item, list2);
if(listFormato.get(posicion).cSeccion.trim().equals("M"))
{
adapterCombo.setDropDownViewResource(R.layout.spinner);
Combo1.setAdapter(adapterCombo);
Combo1.setSelection(listFormato.get(posicion).iIndexCombo);
llTempZZ.addView(Combo1);
Combo1.setGravity(Gravity.CENTER);
llTempXX.addView(llTempZZ);
llTEMPADRE.addView(llTempXX);
pllh.addView(llTEMPADRE);
}
else if(listFormato.get(posicion).cSeccion.trim().equals("U")){
final Spinner Combo0 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
List<String> list3 =new ArrayList<String>();
list3.add("SI");
list3.add("NO");
ArrayAdapter<String> adapterCombo3 = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
,android.R.layout.simple_spinner_item, list3);
adapterCombo3.setDropDownViewResource(R.layout.spinner);
Combo0.setAdapter(adapterCombo3);
llTempZZ.addView(Combo0);
llTempXX.addView(llTempZZ);
//llTempXX.addView(llTempAA);
llTEMPADRE.addView(llTempXX);
pllh.addView(llTEMPADRE);
Combo0.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView,
View view, int pos, long id) {
if (pos == 0) {
pll2.setVisibility(View.VISIBLE);
}
else if(pos==1) {
pll2.setVisibility(View.GONE);
}
listFormato.get(posicion).vResultado = Combo0.getSelectedItem().toString();
RECORDCARDITEM_DAO.Actualizar(FichaRutasVerif2SeccionSlideFragment.this.getActivity(), listFormato.get(posicion));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
else if(listFormato.get(posicion).cSeccion.trim().equals("T"))
{
adapterCombo2.setDropDownViewResource(R.layout.spinner);
Combo2.setAdapter(adapterCombo2);
//Combo1.setSelection(listFormato.get(posicion).iIndexCombo);
llTempZZ.addView(Combo2);
Combo2.setGravity(Gravity.CENTER);
llTempXX.addView(llTempZZ);
llTEMPADRE.addView(llTempXX);
//////TEXTOS
Combo2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView,
View view, int pos, long id) {
if(Combo2.getSelectedItemPosition()==0)
{
}
if (pos ==12) {
llTempAA.setVisibility(View.VISIBLE);
} else {
llTempAA.setVisibility(View.GONE);
}
listFormato.get(posicion).vResultado = Combo2.getSelectedItem().toString();
RECORDCARDITEM_DAO.Actualizar(FichaRutasVerif2SeccionSlideFragment.this.getActivity(), listFormato.get(posicion));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
You can do something like this
Spinner firstSpinner = (Spinner) findViewById(R.id.first_spinner);
// You can set your first spinner values here if not set already, and then
firstSpinner.setOnItemSelectedListener(onFirstSpinnerChange(firstSpinner));
And the further add this
AdapterView.OnItemSelectedListener onFirstSpinnerChange(Spinner mySpinner) {
return new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// You can add this method to set the values of the second spinner in your activity
// You can use the position, or alternatively selectedItemView for this purpose to identify the selected item
setSecondSpinnerBasedOnFirstOnePositionSelected();
// Or you can just set your spinner in this block
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// do nothing
}
};
}
I have 3 spinners in my activity and I'm loading data for these 3 spinners from json response.
1st spinner(crustSP) gets data straight forward from the json response.
2nd spinner's (SizeSP) data depends on what selected from 1st spinner and
3rd spinner's (extraDescriptionOneSP) data depends on what select from 2nd spinner.
My question is it crashes inside 2nd SP because I haven't selected any value inside that when activity starting (I have shown that inside the code as a comment), how can I overcome that? where should I have that piece of code. It goes on without crashing when I hard code the position of sizeSp.
crust = Utils.removeDuplicatesFromList(crust);
ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, crust);
dataAdapterCru
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crustSP.setAdapter(dataAdapterCru);
crustSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this) {
public void onNothingSelected(AdapterView<?> arg0) {
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String crustSelectedItem = crustSP.getSelectedItem()
.toString();
List<String> resultDescription = getFilteredDescriptions(
crustSelectedItem, sizeDescription);
sizeSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this) {
public void onNothingSelected(AdapterView<?> arg0) {
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String sizeSelectedItem = sizeSP
.getSelectedItem().toString(); // it crashes here since I haven't selected any value
List<String> resultTopping = getFilteredToppings(
sizeSelectedItem, topDescription);
extraDescriptionOneSP
.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this));
ArrayAdapter<String> dataAdapterExtraDesOne = new ArrayAdapter<String>(
ActivityPizzaCustomize.this,
android.R.layout.simple_spinner_item,
resultTopping);
dataAdapterExtraDesOne
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
extraDescriptionOneSP
.setAdapter(dataAdapterExtraDesOne);
extraDescriptionOneSP
.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterExtraDesOne,
R.layout.contact_spinner_row_nothing_selected,
ActivityPizzaCustomize.this));
}
});
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
ActivityPizzaCustomize.this,
android.R.layout.simple_spinner_item,
resultDescription);
dataAdapterDes
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizeSP.setAdapter(dataAdapterDes);
sizeSP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterDes,
R.layout.contact_spinner_row_nothing_selected,
ActivityPizzaCustomize.this));
}
});
This is how I filter the items,
List<String> getFilteredDescriptions(String crustSelectedItem,
List<String> sizeDescription) {
List<String> resultDescription = new ArrayList<String>();
crustSelectedItem = crustSP.getSelectedItem().toString();
if (sizeDescription == null || sizeDescription.isEmpty())
return resultDescription;
for (int i = 0; i < sizeDescription.size(); i++) {
sizeDescription = Utils.removeDuplicatesFromList(sizeDescription);
if (!sizeDescription.get(i).contains(crustSelectedItem))
continue;
resultDescription.add(sizeDescription.get(i));
}
return resultDescription;
}
List<String> getFilteredToppings(String sizeSelectedItem,
List<String> topDescription) {
List<String> resultTopping = new ArrayList<String>();
sizeSelectedItem = sizeSP.getSelectedItem().toString();
if (topDescription == null || topDescription.isEmpty())
return resultTopping;
for (int i = 0; i < topDescription.size(); i++) {
topDescription = Utils.removeDuplicatesFromList(topDescription);
if (!topDescription.get(i).contains(sizeSelectedItem))
continue;
resultTopping.add(topDescription.get(i));
}
return resultTopping;
}
You have to call onItemSelectedListner inside the other one, Like,,
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Arraylist2.add((String)spinner1.getItemAtPosition(position));
//Some Code/Ligic here
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position2, long id) {
// TODO Auto-generated method stub
Arraylist3.add((String) spinner2.getItemAtPosition(position2));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub111
}
});
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Hope it helps...
just give it try..
put this code before setting onItemSelectedListner..
spin1.setSelection(0, false);
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
}
});
Spinner is not working. Here is my code:
public class second extends Activity{
Spinner spin;
String[] str;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
spin = (Spinner) findViewById(R.id.spin);
str = new String[] {"bike","car"};
spin.setAdapter(new ArrayAdapter<CharSequence>(this, android.R.layout.simple_dropdown_item_1line, str));
spin.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int pos,
long id) {
// TODO Auto-generated method stub
String data= spin.getItemAtPosition(pos).toString();
Toast.makeText(second.this, data, Toast.LENGTH_SHORT).show();
}
});
}
}
The only mistake you made is you have set different event listener..
The Listener OnItemClickListener is for ListView..
For Spinner you have to set OnItemSelectedListener ..
Try replacing OnItemClickListener to setOnItemSelectedListener like :
spin.setOnItemSelectedListener (new OnItemSelectedListener() {
public void onItemClick(AdapterView<?> adapter, View view, int pos,
long id) {
// TODO Auto-generated method stub
String data= spin.getItemAtPosition(pos).toString();
Toast.makeText(second.this, data, Toast.LENGTH_SHORT).show();
}
});
The problem is here,
spin.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int pos,
long id) {
// TODO Auto-generated method stub
String data= spin.getItemAtPosition(pos).toString();
Toast.makeText(second.this, data, Toast.LENGTH_SHORT).show();
}
});
Try replacing it as follows,
spin.OnItemSelectedListener (new OnItemSelectListener() {
public void onItemSelect(AdapterView<?> adapter, View view, int pos,
long id) {
// TODO Auto-generated method stub
String data= spin.getItemAtPosition(pos).toString();
Toast.makeText(second.this, data, Toast.LENGTH_SHORT).show();
}
});
Here is the problem in your oncreate() method:
spin = (Spinner) findViewById(R.id.spin);
You assign the result to spinner which is a local variable/reference here. main.spinner class field is still null. This is why you have NullPointerException in calculate() method.
Replace the line above with:
this.spin = (Spinner) findViewById(R.id.spin);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,androidBooks);
spin.setAdapter(adapter);
Try this one
spinner = (Spinner) findViewById(R.id.spinner);
List<String> categories = new ArrayList<String>();
categories.add("--Qualification--");
categories.add("High School");
categories.add("Higher Secondary/PUC");
categories.add("Diploma");
categories.add("Degree");
categories.add("Master Degree");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.spinnertext, categories);
adapter.setDropDownViewResource(android.R.layout.select_dialog_item);
spinner.setAdapter(adapter);
Check Out on this link
placeSpinner.getSelectedItem().toString();
so I'm using this coding lines to get the text from my spinner and pass it on to another java file. But the application keeps giving me a force close whenever I do this.
How can I resolve this error?
Thank you so much.
I hope it will helpful to you.
Try this Code...
Here i store the index and value of selected spinner into SharedPreferences
private SharedPreferences prefs;
private String prefName = "spinn";
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<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
int pos1=position;
String str1=sp1.getSelectedItem().toString();
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//---save the values in the EditText view to preferences---
editor.putInt("index", pos1);
editor.putString("value", str1);
//---saves the values---
editor.commit();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
To get that value into another java file, inside that java file write below code where ever you need it..
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
int pos=prefs.getInt("index", (Integer) null);
String value=prefs.getString("value", "");
Try this code : set adapter to the spinnerName as:
spinnerName=(Spinner)findViewById(R.id.spinnerName);
String []data={"item1","item2","item3","item4","item5"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, data);
spinnerName.setAdapter(adapter);
Now set setOnItemSelectedListener to spinnerName.
spinnerName.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), spinnerName.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
// pass value of selected spinnerItem to other Activity.
intent.putExtra("spinnerItem", spinnerName.getSelectedItem().toString());
startActivity(intent);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Sorry, I posted it wrong.
Pls try this.
String []data={"item1","item2","item3","item4","item5"};
spinner.setAdapter(adaptador);
spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
android.view.View v, int position, long id) {
mData.setText("Selected: " + data[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
mData.setText("");
}
});
Use this:
final String text = ((YourAdapter) spinner.getAdapter()).getItem(spinner.getSelectedItemPosition());
This should work:
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
String value = (TextView)view.getText().toString();
}
final Equipamento[] eqptos = eqptoDao.list();
List<String> labels = new ArrayList<String>();
for(Equipamento eqpto : eqptos)
{
labels.add(eqpto.getDescricao());
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,labels);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
//to get value of spinner
eqptos[Integer.parseInt("" + spinner.getSelectedItemId())].getEquipamento()