Writing an ArrayAdapter dirrently - android - android

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));

Related

One spinner showing correctly a default value and the other not

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.

setting value from database to multiple spinner doesn't work?

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

ArrayAdapter.createFromResource issue

I am trying to make "Select one" on Spinner. I saw all answer regarding this subject but I am still having some issues. Usual way of making custom spinner is:
ArrayAdapter<CharSequence> dataAdapter1 = ArrayAdapter.createFromResource(this, R.array.entries,
android.R.layout.simple_spinner_item);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter1);
spinner1.setAdapter(
new NothingSelectedSpinnerAdapter(
dataAdapter1,
R.layout.contact_spinner_row_nothing_selected,
this));
In this code I have to define R.array.entries in Strings.xml, but my app is populating spinner from MySQL and I am having a list grad[i]=json.getString("Grad");. How can I create this ArrayAdapter.createFromResource with that list instead of Entries that are defined in Strings.xml? Tnx
Query the data, put it in a List or Array and use this constructor of Array Adapter
ArrayAdapter<CharSequence> dataAdapter1 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, yourArrayOrList);
More here: http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context, int, java.util.List)

Android Preference menu(change array id when clicked)

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);
}

Spinner onClick items

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);

Categories

Resources