Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - NumberPicker with custom string data shows wrong value?

It is so simple, and it is so hard at the same time.

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String[] values = {"d", "aa", "a", "b"};
    NumberPicker numberPicker = findViewById(R.id.number_picker);
    numberPicker.setDisplayedValues(values);
    numberPicker.setMinValue(0);
    numberPicker.setMaxValue(3);
    numberPicker.setValue(2); // Want to show "a" in number picker
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <NumberPicker
        android:id="@+id/number_picker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Result

enter image description here

Some more cases:

  1. If values are {"d", "c", "a", "b"}, the result is "a".
  2. If values are {"d", "aaaaa", "a", "b"}, the result is "aaaaaa".
  3. If values are {"d", "aa", "c", "b"}, the result is "c".
  4. If values are {"d", "b", "a", "aa"}, the result is "a".
  5. If values are {"d", "AA", "a", "c"}, the result is "AA".
  6. If values are {"d", "ABCDAA, "a", "c"}, the result is "ABCDAA".

And when I click to anywhere inside the number picker, the "aa"'ish becomes "a" as expected. However, try putting numberPicker.performClick() after the setValue(2) method won't help.

This looks so simple but it is driving me nuts. Please help me.

like image 879
peanut Avatar asked Mar 19 '26 08:03

peanut


1 Answers

tyr to create your own function to modify the data to be set on NumberPicker before set on NumberPicker.This function prevents any string to be substring of any string from the given array to be set on NumberPicker

modifyDataForNumberPicker(values);
numberPicker.setDisplayedValues(values);

private void modifyDataForNumberPicker(String[] dataList){
        int i=0;
        for(String data : dataList){
            int pos = i++;
            dataList[pos] = data+" ";
    }
} 

the get the value clicked in the NumberPicker

numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
    String selectedvalue = values[newVal]           
    }
});
like image 106
ismail alaoui Avatar answered Mar 21 '26 20:03

ismail alaoui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!