Reputation: 47
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.
Upvotes: 1
Views: 2769
Reputation: 9408
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 :)
Upvotes: 0
Reputation: 3260
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
Upvotes: 1