Question

I am trying dynamically populate an array with values based on user input. The user has to input a number e.g. 4, and the spinner has to display values (1,2,3,4). So I am using this method:

    Integer[] items = new Integer []{0,1};

int size;

   no_of_items_.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub



                    size= (int) Float.parseFloat(no_of_items_.getText().toString());
                    items = new Integer [size];

                    int i=1;
                    for (i=1;i==size;i++)
                    {

                        items[i]=i;


                    }

                    populatespinner();
                }


    public void populatespinner() 
{
    ArrayAdapter<Integer> ItemNumberadapter = new ArrayAdapter<Integer>   (this,android.R.layout.simple_spinner_item, items);

    ItemNumberadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    item_no.setAdapter(LayerNumberadapter);



}

Now the problem is that when I run the code, the spinner has the same number of items as the user has input, but the rows of the spinner are completely blank. LogCat shows this warning: "Text is not set due to it's null" Any help in this regard will be appreciated, android noob here. :-)

Was it helpful?

Solution

Can you try the loop with i starting by 0 not 1 and see if it helps ?

for (i=0;i<size;i++)
{
 items[i]=i;
 }

OTHER TIPS

ArrayList<Integer> Options = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {

  Options.add(i, i+1);

 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top