アプリケーションに場所を示すスピナーがあります。現在の場所、または入力する必要のある自分で選択した場所のいずれか。またはそうする必要がありますこれの良い例を見つけることができるのはGoogleプレイスです。それが私が達成したいことです、多かれ少なかれ...
そのため、アプリを最初に作成する場合は、ユーザーの現在の場所を取得し、カスタムアレイアダプターの場所プロパティをその場所に設定します。その後、drawablestateを更新します。アダプタのgetViewで、テキストを白に変更し、テキストをlocationプロパティに設定します。これは正常に機能します。
別の場所を選択すると、場所を入力するための入力ダイアログが表示されます。完了したら、アダプターのlocationプロパティを更新し、スピナーにdrawableStateを更新させます。その後、アダプタにgetView関数を再度入力し、テキストの色とテキスト自体を編集しますが、スピナーのテキストは同じままです。スピナーをもう一度押して「現在の場所」を押した後でのみ、以前に指定した場所が表示されます。もう一度「現在の場所」を押すと、何も実行されません(場所は取得されますが、ビューは更新されません)。もう一度「別の場所」を押すと、現在の場所が表示され、別の場所を尋ねられますが、これも同じです。
誰かがこれを解決する方法のアイデアを持っていますか?私のコードについては、以下を参照してください。
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();
}
}
私のカスタム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;
}
}