I'm trying to populate the spinner, but the application crashes on spiner.setAdapter(adapter)...
final Context c=this;
ArrayList<CountryItem> countriesArray = GetCountries1();
ArrayAdapter<CountryItem> adapter = new ArrayAdapter<CountryItem>(
c,android.R.layout.simple_spinner_item, countriesArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final Spinner spiner=(Spinner)findViewById(R.id.spinner1);
spiner.setAdapter(adapter);
I can't find any errors here, and eclipse debug mode does not show anything usefull...
Here is the example from google...is it so much different than my code?
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
What error do you get?
Did you check if the countriesArray has elements or if it is empty?
Is it working when you use, e.g., a simple string array? If yes, then the problem lies with the CountryItem objects.
Other than that, the only difference I see is that you pass the context as a final variable, but I don't know if this causes the error. Try to pass just this and see if it works.
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 have an activity that recieves two variables called l_source and l_destination and I want to set the default values of two spinners equal to those variables. To do so, I have made a search and I understand that I need to get the position of the value of the variable (will give a number) and then set the new position of the spinner. The problem is that I am trying to use getAdapterPosition and I am sent Cannot resolve method getAdapterPosition(l_source).
EDIT
I finally managed to do it, but it is only working in one spinner. Have you got any idea of why? 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));
Thank you for your time.
get position of selected item is spinner by int spinnerPosition = spinner.getSelectedItemPosition();
I have made various string-arrays in the string.xml file and I have to set different arrays as entries for the spinner according to certain condition in Java. Is it possible or is database the only way to do so. Thanks in advance.
You need to use an adapter and populate with tha array in xml file.
Specify the name of your array in xml at createFromResource method (second parameter).
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.my_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
You have to extract your data from file:
String[] testArray = getResources().getStringArray(R.array.testArray);
Then, you have to inflate in the spinner:
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, testArray );
mySpinner.setAdapter(spinnerArrayAdapter);
You can start with using ArrayAdapter, it is a simple class to populate spinner items programmatically.
String data[];
//... do your stuff to get populate this array
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, data);
mySpinner.setAdapter(spinnerArrayAdapter);
You can also modify the view of dropdown items and customize them further by overriding this class.
I have reviewed several examples of how to set up a spinner in my program and they all use the same format that I am using, yet eclipse says that it is a syntax error, specifically: "Syntax error on token "setDropDownViewResource", identifier expected after this token. Additionally it says that the statement that sets the spinner adapter expects a variable declaratorid after the token "adapter"
here is the code block in question:
Spinner tipPercent = (Spinner) findViewById(R.id.tip_spinner);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this,
R.array.tip_percentage, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
What am I missing?
Initialize adapter ArrayAdapter instance as:
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this,
R.array.tip_percentage, android.R.layout.simple_spinner_item);
In my main activity in the onCreate method I am trying to add a string array from the R.array file to an array adapter. When i go to launch the app the drop down box is empty. If I manually create a String array via code the drop down is fully populated. Here is the code below.
ArrayAdapter<String> dateAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, R.array.reminderDays);
dateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dateRangeSpinner.setAdapter(dateAdapter);
dateRangeSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
Try,
ArrayAdapter<CharSequence> dateAdapter =ArrayAdapter.createFromResource(this, android.R.layout.simple_spinner_item, R.array.reminderDays);
This is working fine for me.