我在显示位置的应用程序中有一个微调器。无论是您当前的位置还是您必须输入的自己选择的位置。或者如此,应该可以找到一个很好的例子,就是Google Places。这是我想要实现的,或多或少......如何在微调器中显示位置而不是在Android中选择的选项?
因此,如果应用程序是第一次创建,我抓取用户的当前位置,并将我的自定义arrayadapter的位置属性设置为该位置。之后,我刷新drawablestate。在适配器的getView中,我将文本更改为白色,并将文本设置为位置属性。这工作正常。
当我选择不同的位置时,我得到一个输入对话框来输入我的位置。一旦完成,我更新适配器的位置属性,并使微调器刷新它的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;
}
}