如何在spinner中显示位置,而不是在Android中显示所选选项?

如何在spinner中显示位置,而不是在Android中显示所选选项?,android,spinner,Android,Spinner,我的应用程序中有一个显示位置的微调器。您必须输入的当前位置或您自己选择的位置。或者你可以找到一个很好的例子,Google Places。这就是我想要实现的,或多或少 因此,如果第一次创建应用程序,我将获取用户的当前位置,并将自定义arrayadapter的location属性设置为该位置。之后,我刷新drawablestate。在适配器的getView中,我将文本更改为白色,并将文本设置为location属性。这个很好用 当我选择一个不同的位置时,我会得到一个输入对话框来输入我的位置。完成后,我

我的应用程序中有一个显示位置的微调器。您必须输入的当前位置或您自己选择的位置。或者你可以找到一个很好的例子,Google Places。这就是我想要实现的,或多或少

因此,如果第一次创建应用程序,我将获取用户的当前位置,并将自定义arrayadapter的location属性设置为该位置。之后,我刷新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();
}
}
公共类活动扩展活动{
//UI元素
私人微调器(位置微调器),;
专用位置阵列适配器_位置适配器;
//位置
私有字符串[]_locationArray={“当前位置”,“不同位置”};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//配置微调器
configSpinner();
}
//功能
私有void配置微调器(){
_locationSpinner=(Spinner)findViewById(R.id.main\u位置);
_locationAdapter=new LocationArrayAdapter(此,R.layout.spinner\u项,\u locationArray);
_locationAdapter.setDropDownViewResource(R.layout.spinner\u下拉项);
showCurrentLocationInSpinner();
_locationSpinner.setAdapter(_locationAdapter);
_locationSpinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共位置(AdapterView父项、视图、整数位置、长id){
如果(位置==0)
showCurrentLocationInSpinner();
其他的
showOtherLocationInSpinner();
}
@凌驾
未选择的公共无效(AdapterView父项){}
});
}
//视图功能
私有void showCurrentLocationInSpinner(){
试一试{
Location loc=getCurrentLocation();
如果(loc==null)返回;
列表地址=_geo.getFromLocation(loc.getLatitude(),loc.getLatitude(),1);
地址CuraAddress=地址。获取(0);
_locationAdapter.setLocation(curAddress.getAddressLine(0)+“,“+curAddress.getLocation());
_locationSpinner.refreshDrawableState();
}捕获(IOE异常){
e、 printStackTrace();
}catch(IndexOutOfBoundsException e){
e、 printStackTrace();
}
}
受保护的void showOtherLocationInSpinner(){
AlertDialog.Builder alert=新建AlertDialog.Builder(此);
setMessage(getResources().getString(R.string.location_对话框_消息));
//设置EditText视图以获取用户输入
最终编辑文本输入=新编辑文本(本);
alert.setView(输入);
alert.setPositiveButton(getResources().getString(R.string.dialog_ok),new DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
可编辑值=input.getText();
_locationAdapter.setLocation(value.toString());
_locationSpinner.refreshDrawableState();
}
});
alert.setNegativeButton(getResources().getString(R.string.dialog_cancel),new DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
}
});
alert.show();
}
}
我的自定义阵列适配器:

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;
}

}
公共类位置ArrayAdapter扩展了ArrayAdapter{
私有字符串_位置;
public LocationArrayAdapter(上下文上下文,int textViewResourceId,CharSequence[]对象){
超级(上下文、textViewResourceId、对象);
}
公共字符串getLocation(){
返回位置;
}
公共void集合位置(字符串位置){
这个。_位置=位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=super.getView(位置、转换视图、父级);
TextView t=(TextView)view.findViewById(android.R.id.text1);
t、 setTextColor(Color.WHITE);
t、 setText(_位置);
返回视图;
}
}

原来是个愚蠢的错误。。。我只需要通知我的适配器数据集已更改

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;
        notifyDataSetChanged(); //FIXES IT
    }

    @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;
    }

}
公共类位置ArrayAdapter扩展了ArrayAdapter{
私有字符串_位置;
public LocationArrayAdapter(上下文上下文,int textViewResourceId,CharSequence[]对象){
超级(上下文、textViewResourceId、对象);
}
公共字符串getLocation(){
返回位置;
}
公共void集合位置(字符串位置){
这个。_位置=位置;
notifyDataSetChanged();//修复它
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=super.getView(位置、转换视图、父级);
TextView t=(TextView)view.findViewById(android.R.id.text1);
t、 setTextColor(Color.WHITE);
t、 setText(_位置);
返回视图;
}
}