Bart Bart -4 years ago 105
Android Question

Parsing simple json to spinner entries

I've been trying to get into android/java programming and I've been having issues understanding how to properly get the value of this json and parse it into the options to select in a spinner.

My json is like:

["Result1","Result2","Result3"]


My current code is like:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RequestQueue queue = Volley.newRequestQueue(this);
String url = "https://example.com/jsonfile.json";

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Do something with response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.toString());
}
});

queue.add(stringRequest);
}


What would be the easiest way to get these values (Result1, Result2, Result3, etc.) into the spinner.entries?

Thanks in advance

Answer Source

Try this:

myString.replace("\"]","");
myString.replace("[\"","");

List<String> myList = new ArrayList<String>(Arrays.asList(s.split("\",\"")));


ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(varRoot, android.R.layout.simple_spinner_item, myList);

spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down vieww

mySpinner.setAdapter(spinnerArrayAdapter);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download