Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset data of NumberPicker

I am using two NumberPickers to display province(mProvincePicker) and city(mCityPicker) data in my app. When user changes province data, city data should be changed accordingly. I reset the mCityPicker data in NumberPicker.onValueChange(NumberPicker picker, int oldVal, int newVal). But it does not work well and an java.lang.ArrayIndexOutOfBoundsException crash the app.

Here is my code:

public class AreaPickerDialog extends Dialog implements OnValueChangeListener, OnClickListener {
    static final String TAG = "AreaPickerDialog";

    private NumberPicker mProvincePicker;
    private NumberPicker mCityPicker;
    private Button mCancelBtn;
    private Button mOKBtn;

    private AreaUtil mAreaUtil;
    private List<Area> mProvinces;
    private int mAreaId;
    private int mProvinceId;
    private Handler mHandler;

    public AreaPickerDialog(Context context, Handler handler, int areaId) {
        super(context);
        mAreaUtil = AreaUtil.getInstance();
        mProvinces = mAreaUtil.getProvinceList();
        mAreaId = areaId;
        mProvinceId = mAreaUtil.getProvinceIdByAreaId(areaId);
        mHandler = handler;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_area_picker);
        mProvincePicker = (NumberPicker) findViewById(R.id.np_province);
        mCityPicker = (NumberPicker) findViewById(R.id.np_city);
        mCancelBtn = (Button) findViewById(R.id.btn_cancel);
        mOKBtn = (Button) findViewById(R.id.btn_ok);

        initProvinceData();

        mProvincePicker.setOnValueChangedListener(this);
        mCityPicker.setOnValueChangedListener(this);
        mCancelBtn.setOnClickListener(this);
        mOKBtn.setOnClickListener(this);
    }

    private void initProvinceData() {
        if (mProvinces != null && mProvinces.size() > 0) {
            String[] provinceNames = new String[mProvinces.size()];
            for (int i = 0; i < provinceNames.length; i++) {
                provinceNames[i] = mProvinces.get(i).getProvincename();
            }
            mProvincePicker.setDisplayedValues(provinceNames );
            mProvincePicker.setMinValue(1);
            mProvincePicker.setMaxValue(mProvinces.size());
        }
        mProvincePicker.setValue(mProvinceId);
        initCityData(mProvincePicker.getValue());
    }

    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.setValue(0);
                mCityPicker.setDisplayedValues(cityNames);
                mCityPicker.setMinValue(min);
                mCityPicker.setMaxValue(max);
            } catch (NumberFormatException e) {
                ILog.e(TAG, e.getMessage(), e);
            }
        }
    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        if (mProvincePicker.equals(picker)) {
            initCityData(mProvincePicker.getValue());
        } else if (mCityPicker.equals(picker)) {
            mAreaId = mCityPicker.getValue();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_cancel:
            dismiss();
            break;

        case R.id.btn_ok:
            Message msg = mHandler.obtainMessage(MineActivity.MSG_UPDATE_AREA);
            msg.arg1 = mAreaId;
            msg.sendToTarget();
            dismiss();
            break;

        default:
            break;
        }
    }
} 

And logcat error trace:

12-30 23:09:26.560: E/InputEventReceiver(5620): Exception dispatching input event.
12-30 23:09:26.560: W/dalvikvm(5620): threadid=1: thread exiting with uncaught exception (group=0x41ddf438)
12-30 23:09:26.580: E/AndroidRuntime(5620): FATAL EXCEPTION: main
12-30 23:09:26.580: E/AndroidRuntime(5620): java.lang.ArrayIndexOutOfBoundsException: length=14; index=15
12-30 23:09:26.580: E/AndroidRuntime(5620):     at net.simonvt.numberpicker.NumberPicker.updateInputTextView(NumberPicker.java:1840)
12-30 23:09:26.580: E/AndroidRuntime(5620):     at net.simonvt.numberpicker.NumberPicker.setDisplayedValues(NumberPicker.java:1423)
12-30 23:09:26.580: E/AndroidRuntime(5620):     at com.meishai.app.dialog.AreaPickerDialog.initCityData(AreaPickerDialog.java:88)
12-30 23:09:26.580: E/AndroidRuntime(5620):     at com.meishai.app.dialog.AreaPickerDialog.onValueChange(AreaPickerDialog.java:100)
12-30 23:09:26.580: E/AndroidRuntime(5620):     at net.simonvt.numberpicker.NumberPicker.notifyChange(NumberPicker.java:1855)
12-30 23:09:26.580: E/AndroidRuntime(5620):     at net.simonvt.numberpicker.NumberPicker.setValueInternal(NumberPicker.java:1641)
12-30 23:09:26.580: E/AndroidRuntime(5620):     at net.simonvt.numberpicker.NumberPicker.scrollBy(NumberPicker.java:1106)
12-30 23:09:26.580: E/AndroidRuntime(5620):     at net.simonvt.numberpicker.NumberPicker.onTouchEvent(NumberPicker.java:886)
12-30 23:09:26.580: E/AndroidRuntime(5620):     at android.view.View.dispatchTouchEvent(View.java:7127)
12-30 23:09:26.580: E/AndroidRuntime(5620):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
12-30 23:09:26.580: E/AndroidRuntime(5620):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
12-30 23:09:26.580: E/AndroidRuntime(5620):     at net.simonvt.numberpicker.NumberPicker.dispatchTouchEvent(NumberPicker.java:944)
    ....

I am reseting the minValue,maxValue and DisplayedValues to reset the NumberPicker data and not working, so what is the right way?

like image 332
iStar Avatar asked Dec 30 '13 15:12

iStar


2 Answers

check for Call setDisplayedValues(null) before setMaxValue in onClick.

Reference: https://stackoverflow.com/a/24322319/2624806

like image 122
CoDe Avatar answered Sep 20 '22 06:09

CoDe


I have the same problem and I fixed it without modifing NumberPicker.java. Here is the code for your reference.

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();
                }
                int maxV = mCityPicker.getMaxValue();
                if (max> maxV){ 
                    mCityPicker.setMinValue(min);
                    mCityPicker.setValue(0);
                    mCityPicker.setDisplayedValues(cityNames);
                    mCityPicker.setMaxValue(max);
                }else{
                    mCityPicker.setMinValue(min);
                    mCityPicker.setValue(0);
                    mCityPicker.setMaxValue(max);
                    mCityPicker.setDisplayedValues(cityNames);
                }

            } catch (NumberFormatException e) {
                ILog.e(TAG, e.getMessage(), e);
            }
        }
    }
like image 26
lu yuan Avatar answered Sep 23 '22 06:09

lu yuan