Ibrahim
Ibrahim

Reputation: 23

NumberPicker with arraylist

I am trying to add a list of data within a numberpicker. I am using the library of: https://github.com/ShawnLin013/NumberPicker for the numberpicker.

This is how it appears in my app:

Image NumberPicker

As you can see it appears with [ ] at its sides and separated by a comma, how can I eliminate that? I guess that's the problem.

I'm getting the information from the FIREBASE database.

My code:

IList = new ArrayList<>();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Information").child( uploads.getIdPost() )
        .child("CBS1");

reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        IList.clear();
        for (DataSnapshot snapshot : dataSnapshot.getChildren()){
            IList.add(snapshot.getKey());

            String[] data = {String.valueOf( IList )};
            numberPicker.setMinValue(data.length);
            numberPicker.setMaxValue(data.length);
            numberPicker.setDisplayedValues(data);
            numberPicker.setValue(data.length);
            numberPicker.setWrapSelectorWheel(false);
        }
    }

Upvotes: 1

Views: 1866

Answers (2)

Son Truong
Son Truong

Reputation: 14173

This line

String[] data = {String.valueOf( IList )};

String.valueOf( IList ) will return a string whose value [CB, CC]. That why you see it on the NumberPicker UI.

Solution: Convert string arraylist to string array.

IList.add(snapshot.getKey());

// Convert string arraylist to string array.
String[] data = new String[list.size()];
data = list.toArray(data);

// Set minValue is 1 instead of data.length
numberPicker.setMinValue(1); 
numberPicker.setMaxValue(data.length);
numberPicker.setDisplayedValues(data);
numberPicker.setValue(data.length);
numberPicker.setWrapSelectorWheel(false);

Update: I think here is what you mean

reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        IList.clear();

        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
            IList.add(snapshot.getKey());
        }

        // Convert string arraylist to string array.
        String[] data = new String[IList.size()];
        data = IList.toArray(data);

        numberPicker.setMinValue(0);
        numberPicker.setMaxValue(data.length - 1);
        numberPicker.setDisplayedValues(data);
        numberPicker.setValue(0);
        numberPicker.setWrapSelectorWheel(false);
    }
}

Upvotes: 3

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

This is because of the following code:

String[] data = {String.valueOf( IList )};

where String.valueOf return the string representation representation of the IList Object.

You need to use something like toArray.

Upvotes: 0

Related Questions