我的应用程序中有一个显示位置的微调器。您必须输入的当前位置或您自己选择的位置。或者应该这样你可以找到一个很好的例子就是谷歌地方。这就是我想要达到的,或多或少...
因此,如果第一次创建应用程序,我会获取用户的当前位置并将我的自定义 arrayadapter 的位置属性设置为该位置。之后,我刷新drawablestate。在我的 Adapter 的 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;
}
}