i got 2 classes. A class with a spinner in it and a preference class. The spinner is set up with a array from strings.xml` s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this, R.array.height_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter1);`
The second class is the preference class with a ListPreference(think it is a a ListPreference)
/kind of a spinner. If u click it and select any of the items from the ListPreference i want the spinner to use another array. Change R.array.height_array to R.array.height2_array or something like that. Possible ?
yes, something like this should work. If you need more than 2 choices, use more ifs
if (prefs == choiceOne){
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this, R.array.height_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter1);
}else{
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this, R.array.height2_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter1);
}
Related
I have got two spinners that receive a default value as a variable from another view. The first one is working fine but the second one not (it shows position 0 as the default value). Here is my code:
Spinner spinner = (Spinner) findViewById(R.id.lista_origen);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getPosition(l_source));
Spinner spinner2 = (Spinner) findViewById(R.id.lista_destino);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(adapter2.getPosition(l_target));
l_target and l_source are the variables and they are correctly sent from the previous view to this one.
I would be grateful if you could find what and why is failing.
Thank you for your time.
I would like to programmatically change a name of one of my spinner item but I am not sure how I can do this.
For example given the code below
Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_list_item_array,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
I would like to
Change spinner ITEM name at position 1 to "NEW TEXT"
You should change the data in R.array.spinner_list_item_array.
After you have changed the data you will need to then call adapter.notifyDataSetChanged() and then adapter.setAdapter(adapter).
CharSequence[] fiilliste = getResources().getStringArray(R.array.spinner_list_item_array);
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item, fiilliste);
spinner.setAdapter(adapter);
fiilliste[1] = "NEW TEXT";
adapter.notifyDataSetChanged();
I am trying to set value from array to spinner but it is working for few and not for others. Please help me out. here is my code.
edmORdelOF = (Spinner) findViewById(R.id.ed_sign_up_manf_del_of);
ArrayAdapter<CharSequence> adapterItem = ArrayAdapter.createFromResource(this,
R.array.Item, android.R.layout.simple_spinner_item);
adapterItem.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
edmORdelOF.setAdapter(adapterItem);
System.out.println(profile.getEdmORdelOF()+"....getPosition..........."+adapterItem.getPosition(profile.getEdmORdelOF()));
edmORdelOF.setSelection(adapterItem.getPosition(profile.getEdmORdelOF()));
org_type = (Spinner) findViewById(R.id.Updatesp_sign_up_manf_org_type);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.Org_type, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
org_type.setAdapter(adapter);
System.out.println(profile.getD_org_type()+"....getPosition..........."+adapter.getPosition(profile.getD_org_type()));
org_type.setSelection(adapter.getPosition(profile.getD_org_type()));
country = (Spinner) findViewById(R.id.Updatesp_sign_up_country);
ArrayAdapter<CharSequence> adapterCountry = ArrayAdapter.createFromResource(this,
R.array.Country, android.R.layout.simple_spinner_item);
adapterCountry.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
country.setAdapter(adapterCountry);
System.out.println(profile.getD_country()+"....getPosition..........."+adapterCountry.getPosition(profile.getD_country()));
country.setSelection(adapterCountry.getPosition(profile.getD_country()));
here,i am getting value from bean object profile and trying to set that values to spinner but state doesn't get the value. sorry every one.i have change it
So I'm writing a program that has three spinners and I want to add an array to each of them. However the code I have at the moment works but is very long so I was wondering if there is an easier/shorter/more efficient way to code it.
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.distanceType,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
distanceSpinner.setAdapter(adapter);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.areaType, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
areaSpinner.setAdapter(adapter2);
ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(
this, R.array.genderType, android.R.layout.simple_spinner_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
genderSpinner.setAdapter(adapter3);
As you can see its basically the the same code repeated 3 times.
Create a factory method to ease writing your custom adapters :
public ArrayAdapter createSimpleAdapter(int itemsArrayId ) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, itemsArrayId,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
return adapter;
}
Then the creations become as easy as :
distanceSpinner.setAdapter(createSimpleAdapter(R.array.distanceType));
areaSpinner.setAdapter(createSimpleAdapter(R.array.areaType));
genderSpinner.setAdapter(createSimpleAdapter(R.array.genderType));
i have created a spinner.could i place my spinner items in a table into my java file and call it (maybe from position) in one onClick method?
code:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.epilogis, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
here i m getting the spinner items from the string.xml..
i think this is what you are looking for:
array_spinner=new String[5];
array_spinner[0]="1";
array_spinner[1]="2";
array_spinner[2]="3";
array_spinner[3]="4";
array_spinner[4]="5";
Spinner s = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);