I have a spinner in my application that shows a location. Either your current location or a location of your own choice that you have to enter. Or so it should You can find a great example of this is Google Places. That is what I want to achieve, more or less…
So if the app is first created, I fetch the current location of the user and set the location-property of my custom arrayadapter to the location. After that, I refresh the drawablestate. In the getView of my Adapter, I change the text to white and set the text to the location property. This works fine.
When I choose for a different location, I get an input dialog to enter my location. Once done, I update the location property of my adapter and make the spinner refresh it’s drawableState. After doing that, I enter my getView function in the adapter again, edit the text’s color and the text itself, but the text in the spinner remains the same. Only after I hit the spinner again and hit ‘current location’, it shows the location I gave it earlier. If I hit ‘current location’ again, it does nothing (it fetches the location, but doesn’t update the view). When I proceed to hit ‘another location’ again, it shows your current location and asks for the other location and it’s the same all over again.
Does anybody have an idea of how to solve this? See below for my code:
public class FooActivity extends Activity {
//UI-elements
private Spinner _locationSpinner;
private LocationArrayAdapter _locationAdapter;
//Location
private String[] _locationArray = {"Current location", "Different location"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//config spinner
configSpinner();
}
//functions
private void configSpinner() {
_locationSpinner = (Spinner)findViewById(R.id.main_location);
_locationAdapter = new LocationArrayAdapter(this, R.layout.spinner_item, _locationArray);
_locationAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
showCurrentLocationInSpinner();
_locationSpinner.setAdapter(_locationAdapter);
_locationSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 0)
showCurrentLocationInSpinner();
else
showOtherLocationInSpinner();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
}
//view functions
private void showCurrentLocationInSpinner() {
try {
Location loc = getCurrentLocation();
if (loc == null) return;
List<Address> addresses = _geo.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
Address curAddress = addresses.get(0);
_locationAdapter.setLocation(curAddress.getAddressLine(0) + ", " + curAddress.getLocality());
_locationSpinner.refreshDrawableState();
} catch (IOException e) {
e.printStackTrace();
} catch (IndexOutOfBoundsException e){
e.printStackTrace();
}
}
protected void showOtherLocationInSpinner() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage(getResources().getString(R.string.location_dialog_message));
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton(getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
_locationAdapter.setLocation(value.toString());
_locationSpinner.refreshDrawableState();
}
});
alert.setNegativeButton(getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
}
My custom ArrayAdapter:
public class LocationArrayAdapter extends ArrayAdapter<CharSequence> {
private String _location;
public LocationArrayAdapter(Context context, int textViewResourceId, CharSequence[] objects) {
super(context, textViewResourceId, objects);
}
public String getLocation() {
return _location;
}
public void setLocation(String location) {
this._location = location;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView t = (TextView) view.findViewById(android.R.id.text1);
t.setTextColor(Color.WHITE);
t.setText(_location);
return view;
}
}
Turns out to be quite the stupid mistake… I just had to notify my Adapter that the dataset had changed…