如何在微调器中显示位置而不是在Android中选择的选项?

时间:2012-04-20 08:23:18

标签: android spinner

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

因此,如果首次创建应用,我会获取用户的当前位置,并将自定义arrayadapter的location-property设置为该位置。之后,我刷新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;
}

}

1 个答案:

答案 0 :(得分:0)

原来是一个非常愚蠢的错误......我只需通知我的适配器数据集已经改变了......

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

}