Well, I look into the source code of NumberPicker.java, find that it will recompute the data and refresh UI in setMinValue(int minValue)
and setMaxValue(int minValue)
. When set the new minValue, it will compute using new minValue and old maxValue, then error occured.
So, I modify a little of the NumberPicker
: set value only in setMinValue(int minValue)
and setMaxValue(int maxValue)
, and update UI in setDisplayedValues(String[] displayedValues)
. When data of NumberPicker
changed, just reset the minValue、maxVaule and displayedValues. Note that setDisplayedValues(String[] displayedValues)
MUST be called at last.
Here is what I modified:
NumberPicker.java
:
public void setMinValue(int minValue) {
if (mMinValue == minValue) {
return;
}
if (minValue < 0) {
throw new IllegalArgumentException("minValue must be >= 0");
}
mMinValue = minValue;
if (mMinValue > mValue) {
mValue = mMinValue;
}
//boolean wrapSelectorWheel = mMaxValue - mMinValue > mSelectorIndices.length;
//setWrapSelectorWheel(wrapSelectorWheel);
//initializeSelectorWheelIndices();
//updateInputTextView();
//tryComputeMaxWidth();
invalidate();
}
public void setMaxValue(int maxValue) {
if (mMaxValue == maxValue) {
return;
}
if (maxValue < 0) {
throw new IllegalArgumentException("maxValue must be >= 0");
}
mMaxValue = maxValue;
if (mMaxValue < mValue) {
mValue = mMaxValue;
}
//boolean wrapSelectorWheel = mMaxValue - mMinValue > mSelectorIndices.length;
//setWrapSelectorWheel(wrapSelectorWheel);
//initializeSelectorWheelIndices();
//updateInputTextView();
//tryComputeMaxWidth();
invalidate();
}
public void setDisplayedValues(String[] displayedValues) {
if (mDisplayedValues == displayedValues) {
return;
}
mDisplayedValues = displayedValues;
if (mDisplayedValues != null) {
// Allow text entry rather than strictly numeric entry.
mInputText.setRawInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
} else {
mInputText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
}
boolean wrapSelectorWheel = mMaxValue - mMinValue > mSelectorIndices.length;
setWrapSelectorWheel(wrapSelectorWheel);
updateInputTextView();
initializeSelectorWheelIndices();
tryComputeMaxWidth();
}
AreaPickerDialog.java
:
private void initCityData(int provinceId) {
List<Arealist> cities = mAreaUtil.getCityListOfProvince(provinceId);
if (cities != null && cities.size() > 0) {
try {
int min = Integer.parseInt(cities.get(0).getAreaid());
int max = Integer.parseInt(cities.get(cities.size() -1).getAreaid());
String[] cityNames = new String[cities.size()];
for (int i = 0; i < cityNames.length; i++) {
cityNames[i] = cities.get(i).getName();
}
mCityPicker.setMinValue(min);
mCityPicker.setMaxValue(max);
mCityPicker.setDisplayedValues(cityNames);
} catch (NumberFormatException e) {
ILog.e(TAG, e.getMessage(), e);
}
}
}
This is an ugly but working solution.