I’m trying to make a spinner with an array that I retrieve from a database, here goes the code I’m trying to use:
Unit unit;
int idx = 1;
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, new Unit[] {
unit = connector.getUnit(idx);
while (connector.getUnit(idx) != null){
new Unit(unit.getunitID(), unit.getunitTypeID(), unit.getorganizationID(), unit.getunitName(), unit.getunitAddress());
unit = connector.getUnit(idx);
idx++;
}
});
connector.getUnit(idx) basically retrieves a Unit object from the database, will return a null value if there are not more units to retrieve. The compiler is complaining in:
unit = connector.getUnit(idx);
as “Syntax error, insert “)” to complete VariableInitializer”
and:
idx++;
as “Syntax error, insert “}” to complete Block”
I think that would be the right code to do what I want to, but somehow I can’t manage to make it work, any idea?
Thanks in advance
You try to create an array of Unit objects. Between “{” and “}” of new Unit[] { } you can have only Unit objects separated with “,”. You have a lot of logic there which doesn’t fit this syntax. You should move all of it outside that initializer section and provide an already created array of Unit objects to ArrayAdapter constructor.