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);
Related
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));
Im filling two spinners with text arrays from an xml resource file. depending on the selection in one spinner i want to fill the other with one of two different arrays from the xml resource file. im having trouble doing this in mono for android and cant find any examples online. please help
try this...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
if(pos==0){
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.arrayone, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
else{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.arraytwo, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
your suggestion was a big help. With some changes, here is what worked for me in mono for android:
I have a spinner name spin3. You can select opt1 or opt2. There is a string array in the resources named opt1 and another named opt2. Then a second spinner (spinner4) changes its content to opt1 or opt2 based on what is selected in spin3.
public void spin3_onItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spin = (Spinner)sender;
string pos = spin.SelectedItem.ToString();
if(pos=="opt1")
{
Spinner spinner = (Spinner)FindViewById(Resource.Id.spinner4);
ArrayAdapter adapter = ArrayAdapter.CreateFromResource this, Resource.Array.opt1, Android.Resource.Layout.SimpleSpinnerItem);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = adapter;
}
else
{
Spinner spinner = (Spinner) FindViewById(Resource.Id.spinner4);
ArrayAdapter adapter = ArrayAdapter.CreateFromResource(this, Resource.Array.opt2, Android.Resource.Layout.SimpleSpinnerItem);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = adapter;
}
}
Then to call the event use this
spin3.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spin3_onItemSelected);
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);
}