David Parks David Parks - 6 months ago 72
JSON Question

Android Spinner not updating with JSON data Volley

For the following code I am using MultiSelectSpinner

So I am trying to update a spinner with JSON location data and it wont update for some reason even when I put it inside onCreate. I know that I'm parsing the JSON correctly because when I ask toast to print out the locations I'm getting I correctly get each and every location. Therefore I think the problem lies when I create my MultiSelectSpinner and try to setItems. However, the MultiSelectSpinner class has a constructor for setting items with a String list so I'm puzzled. I would love to know if anyone spots any glaring errors or has any advice. I've also tried to use ArrayAdapter with no luck.

private List<String> locationList = new LinkedList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buyer_profile_edit);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

RequestQueue mRequestQueue;
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024);

Network network = new BasicNetwork(new HurlStack());
final List<String> locationsProvided = new LinkedList<String>();
mRequestQueue = new RequestQueue(cache, network);
mRequestQueue.start();

final String locationURL = "http://10.0.2.2:3000/apip/locations";
// Testing for Valid JSON Web Token
mRequestQueue.add(new JsonArrayRequest(locationURL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for(int i = 0; i < response.length(); i++) {
JSONObject locObj = response.getJSONObject(i);
String location = locObj.getString("location");
locationList.add(location);
Toast.makeText(BuyerProfileEdit.this,location,Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}));

MultiSelectSpinner mySpin = (MultiSelectSpinner)findViewById(R.id.locationDropDownBuyer);
mySpin.setItems(locationList);


Button saveProfile = (Button) findViewById(R.id.submitProfile);

saveProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateBuyerProfile();
}
});


}


Edit: After moving my setItems(locationList) to right after I parse the data inside of onResponse, it works perfectly!

Answer

Move setItems just after parsing all data

try {
                            for(int i = 0; i < response.length(); i++) {
                                JSONObject locObj = response.getJSONObject(i);
                                String location = locObj.getString("location");
                                locationList.add(location);
                                Toast.makeText(BuyerProfileEdit.this,location,Toast.LENGTH_LONG).show();
                            }
                           MultiSelectSpinner mySpin = (MultiSelectSpinner)findViewById(R.id.locationDropDownBuyer);
        mySpin.setItems(locationList);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }