I've a Spinner whose first item is "Select one". In its onTouchListener I've eliminated the first item from the array used for the ArrayAdapter. Now I want the Spinner to open his menu directly inside listener without a second touch from users.
This is where I create spinner:
spinnerCategoria = (Spinner) findViewById(R.id.spinnerCategoria);
spinnerCategoriaArray = new ArrayList<String>();
spinnerCategoriaArray=CpmAnalysisParams.categoria();
spinnerCategoriaArrayAdapter = new ArrayAdapter<String>(this, R.layout.spinner, spinnerCategoriaArray);
spinnerCategoriaArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCategoria.setAdapter(spinnerCategoriaArrayAdapter);
spinnerCategoria.setSelected(true);
spinnerCategoria.setOnTouchListener(Spinner_OnTouch);
spinnerCategoria.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Spinner spinnerOrdinata=(Spinner) findViewById(R.id.spinnerOrdinata);
if (arg0.getItemAtPosition(arg2).equals("Grafico a torta")){
spinnerOrdinata.setEnabled(false);
}
else{
spinnerOrdinata.setEnabled(true);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
This is his ontouchlistener:
private View.OnTouchListener Spinner_OnTouch = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
spinnerCategoriaArrayAdapter=null;
Spinner spinnerCat=(Spinner)findViewById(R.id.spinnerCategoria);
spinnerCategoriaArray.remove(0);
spinnerCategoriaArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner, spinnerCategoriaArray);
spinnerCategoriaArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCat.setAdapter(spinnerCategoriaArrayAdapter);
spinnerCat.setOnTouchListener(null);
}
return true;
}
};
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
}
};
}
Hey i would like to ask how i can pass a selected value from 1 spinner to the next. Example if:
Spinner 1 = "School" is selected
Spinner 2 = Shows sub items for the selected item "School"
OR
Spinner 1 = "Office" is selected
Spinner 2 = Shows sub items for the selected item "Office".
Bind data in second spinner (Spinner 2)in onItemSelected listner of first spinner (Spinner 1)
Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// bind data in second spinner as per you select
// in this first spinner (Spinner 1)
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Check out this sample code , this will surely help you out
public class MainActivity extends Activity {
Spinner s1,s2;
Button btn;
String s;
TextView tv;
ArrayAdapter<String> adap1,adap2,adap3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1=(Spinner)findViewById(R.id.spinner1);
s2=(Spinner)findViewById(R.id.spinner2);
btn=(Button)findViewById(R.id.button1);
tv=(TextView)findViewById(R.id.tv1);
String[] v1=getResources().getStringArray(R.array.c1);
String[] v2=getResources().getStringArray(R.array.c2);
String[] v3=getResources().getStringArray(R.array.c3);
adap1=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v1);
adap2=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v2);
adap3=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v3);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ItemSelectedListenerr implements OnItemSelectedListener {
String[] v=getResources().getStringArray(R.array.c1);
public String s;
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
s=arg0.getItemAtPosition(arg2).toString();
if(arg2==0)
{
s2.setAdapter(adap1);
}
if(arg2==1)
{
s2.setAdapter(adap2);
}
if(arg2==2)
{
s2.setAdapter(adap3);
}
tv.setText(s);
Log.i("hahaha", "item selected is"+s);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}
It will work like charm 100 %. :D
I have a Spinner,Edit Text, Button and a List View in a View. I want to Load my ListView from database Depends on the value from spinner. and i'm doing add operation on List View also.
My Doubt is that i wanted to know how can i load my list view depends on the data from spinner?
public class myClass extends ListActivity implements OnClickListener {
DatabaseFAM db;
Spinner No;
String Selected;
EditText user;
Button btnAdd;
String strselected;
ArrayList<String> list =new ArrayList<String>();
ArrayAdapter<String> adapterLV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myews);
db = new DatabaseFAM(getApplicationContext());
initVar();
}
private void initAudit2Var() {
No = (Spinner) findViewById(R.id.station);
user = (EditText) findViewById(R.id.er);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
adapterLV = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, list);
setListAdapter(adapterLV);
final DatabaseFAM db = new DatabaseFAM(getApplicationContext());
List<String> station = db.getAllstation();
ArrayAdapter<String> stationAdapter = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_spinner_item,
station);
stationAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
No.setAdapter(stationAdapter);
No.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Selected = arg0.getItemAtPosition(
arg2).toString();
list.clear();
list=db.getTag(Selected);
adapterLV.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
you should set Adapter of ListView at the Item selection of the Spinner....
mSpinnerLangAttrValue.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
** Add ListAdapter Here **
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
add onItemSelected event to your spinner.
Depending upon the value of the seletion modify your listView with the required data
use Datasetchanged event on the listview
sp1 = (Spinner)findViewById(R.id.spinner1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// Load your list here
}}
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
}
});
I'm trying to get the position (number) of the spinner when selected to use it in another Activity that will display a different map each time depending on the item selected. when I run the application it crashes. this is the first Activity code:
public class TestProjectActivity extends Activity {
public Spinner spinner1;
public Integer number;
private Button valideButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MySpinner();
valide_button();
}
public void MySpinner() {
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.num, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
// Object item = parentView.getItemAtPosition(position);
TestProjectActivity.this.number = spinner1
.getSelectedItemPosition() + 1;
}
public void onNothingSelected(AdapterView<?> arg0) {// do nothing
}
});
}
public void valide_button() {
valideButton = (Button) findViewById(R.id.valide_button);
valideButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TestProjectActivity.this,
MetroMapActivity.class);
startActivity(intent);
}
});
}
}
The way to get the selection of the spinner is:
spinner1.getSelectedItemPosition();
Documentation reference:
http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()
However, in your code, the one place you are referencing it is within your setOnItemSelectedListener(). It is not necessary to poll the spinner, because the onItemSelected method gets passed the position as the "position" variable.
So you could change that line to:
TestProjectActivity.this.number = position + 1;
If that does not fix the problem, please post the error message generated when your app crashes.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = findViewById(R.id.button);
spinner = findViewById(R.id.sp_item);
setInfo();
spinnerAdapter = new SpinnerAdapter(this, arrayList);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//first, we have to retrieve the item position as a string
// then, we can change string value into integer
String item_position = String.valueOf(position);
int positonInt = Integer.valueOf(item_position);
Toast.makeText(MainActivity.this, "value is "+ positonInt, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
note: the position of items is counted from 0.
final int[] positions=new int[2];
Spinner sp=findViewByID(R.id.spinner);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText( arg2....);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
if (position ==0) {
if (rYes.isChecked()) {
Toast.makeText(SportActivity.this, "yes ur answer is right", Toast.LENGTH_LONG).show();
} else if (rNo.isChecked()) {
Toast.makeText(SportActivity.this, "no.ur answer is wrong", Toast.LENGTH_LONG).show();
}
}
This code is supposed to select both check boxes.
Is there a problem with it?