1

I have a spinner, the drop down list has the correct options from my WebService, but when I selected one option, this have not shown in the Spinner field.

My ArrayAdapter definition...

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
                             (this, android.R.layout.simple_spinner_item,catTorneo);  

catTorneo has the options, it's definition as ArrayList

If I change The ArrayAdapter with List object, the Spinner correct fine.

 List<String> list = new ArrayList<String>();
                list.add("Android");
                list.add("Java");
                list.add("Spinner Data");
                list.add("Spinner Adapter");
                list.add("Spinner Example");

            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
                             (this, android.R.layout.simple_spinner_item,list);    

catTorneo Definition

ArrayList<String> catTorneo ;

In onTaskComplete method I populate catTorneo as..

try {
                ljsonArray = ljsonObject.getJSONArray(tag);
                for (int i = 0; i < ljsonArray.length(); i++) {
                    ljsonObject = ljsonArray.getJSONObject(i);                  
                    // Llena el spinner con el nombre de c/u de los nombres de torneos
                    String ls = ljsonObject.optString("tor_nombre");
                    catTorneo.add(ljsonObject.optString("tor_nombre"));

                }
            } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }

What happened?, why with ArrayList object doesn't work fine and with List object work fine.

2
  • show the declaration and definition for catTorneo Commented Dec 18, 2014 at 17:08
  • Oks, up put declaration and definition Commented Dec 18, 2014 at 17:21

2 Answers 2

1

Since your list is full of String why dont you make things simpler?

You can use this

Spinner mySpinner = (Spinner)findViewById(R.id.yourSpinner'sId);
String[] myItems = {"Android", "Java", "Spinner Data", "Spinner Adapter", "Spinner Example"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item_1, myItems);
mySpinner.setAdapter(adapter);

If you have more than a string then you gonna have to make your own adapter

Sign up to request clarification or add additional context in comments.

Comments

0

I guess your changes to the ArrayList has not been notified to the spinner.

What i would do?

In onTaskComplete method

 ljsonArray = ljsonObject.getJSONArray(tag);
 for (int i = 0; i < ljsonArray.length(); i++) {
         ljsonObject = ljsonArray.getJSONObject(i);                  
         // Llena el spinner con el nombre de c/u de los nombres de torneos
         String ls = ljsonObject.optString("tor_nombre");            
         catTorneo.add(ljsonObject.optString("tor_nombre"));
 }
 //add this line
 dataAdapter.notifyDataSetChanged(); //tells the spinner that data has changed
 //put a toast here so that ull know when the data has be added to it :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.