Search code examples
javaandroidarraylistnumberpicker

Android NumberPicker with strings


I have customised the NumberPicker to show text. The output is this: This

when I press OK I want to add the e.x mouse to my list "Article". What I'm getting instead is the index value (int).

Its populated by "array[]", it includes a list with the data shown. What's the workaround to get my item/string.?

  public void Generatortable(){

            final android.support.v7.app.AlertDialog.Builder alert = new android.support.v7.app.AlertDialog.Builder(this);
            alert.setCancelable(false); 

            LinearLayout l1 = new LinearLayout(getApplicationContext());              

            l1.setOrientation(LinearLayout.HORIZONTAL);             

            final NumberPicker artikkler = new NumberPicker(this);                

            String array[] = new String[responseList.size()];
            for(int j =0;j<responseList.size();j++){
                array[j] = responseList.get(j);
            }
            artikkler.setMaxValue(responseList.size() - 1);
            artikkler.setMinValue(0);
            artikkler.setDisplayedValues(array);
            artikkler.setWrapSelectorWheel(true);        

            l1.addView(artikkler);             

            alert.setView(l1);       

            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {    
                     Article.add(String.valueOf(artikkler.getValue()));

                    lviewAdapter.notifyDataSetChanged();
                    lview.setAdapter(lviewAdapter);  
                }
            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                }
            });     

            alert.show();
        }

Solution

  • Change the line

    Article.add(String.valueOf(artikkler.getValue()));
    

    By

    Article.add(responseList.get(artikkler.getValue());