1
I have the following code that works well, but with an example of array:
@Override
protected Boolean doInBackground(String...urls) {
try {
Log.e("****** MESSAGE ******", " Json Object = " + JSONParser.getJSONFromUrl(URL).get("ReportDetailTextList"));
//
// por favor note que, já consigo pegar os JSON da URL sem problemas! como no exemplo: <br>
// REOTORNO DO JSON:
// {"ReportRetryNumber":4,"ReportDetailTextList":["alfa","beta","gama","yota","zeta"]}
}
} catch (JSONException e) {
e.printStackTrace();
}
// TEST: Temporaly list
List < String > categories = new ArrayList < String > ();
categories.add("aaa");
categories.add("bbb");
categories.add("ccc");
categories.add("ddd");
categories.add("eee");
// Adapter Creation
dataAdapter = new ArrayAdapter < String > (getBaseContext(), android.R.layout.simple_spinner_dropdown_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
return false;
}
I tried to convert to work with JSON and my code got like this:
@Override
protected Boolean doInBackground(String...urls) {
try {
Log.e("****** MESSAGE ******", " Json Object = " + JSONParser.getJSONFromUrl(URL).get("ReportDetailTextList"));
//
// por favor note que, já consigo pegar os JSON da URL sem problemas! como no exemplo: <br>
// REOTORNO DO JSON:
// {"ReportRetryNumber":4,"ReportDetailTextList":["alfa","beta","gama","yota","zeta"]}
List < JSONObject > categories = new ArrayList < > ();
jsonarray = (JSONArray) JSONParser.getJSONFromUrl(URL).get("ReportDetailTextList");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
categories.add(jsonobject);
}
} catch (JSONException e) {
e.printStackTrace();
}
// Adapter Creation
dataAdapter = new ArrayAdapter < String > (getBaseContext(), android.R.layout.simple_spinner_dropdown_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
return false;
}
It turns out I’m not able to put the URL data in my Spinner
,
for example, the line:
dataAdapter = new Arrayadapter(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, Categories);
Accuses an error in the variable Categories ...
So here’s my question:
- 1) How to read json correctly?
- 2) How to put the data in the spinner?
Thank you!
Camila! I’m back! You have to put in your question how is the structure of your JSON.
– viana
WELCOME... . RSRSR good, I put in the comment "{"Reportretrynumber":4,"Reportdetailtextlist":["alpha","beta","gamma","yota","zeta"]}"
– Camila Yamamoto
but soh a detail, the error. so at first... seems to be on the line _dataAdapter = new Arrayadapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, Categories); for to var Categories turns red and is not accepted...
– Camila Yamamoto