How to popular a Spinner from data returned from a URL?

Asked

Viewed 216 times

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.

  • WELCOME... . RSRSR good, I put in the comment "{"Reportretrynumber":4,"Reportdetailtextlist":["alpha","beta","gamma","yota","zeta"]}"

  • 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...

1 answer

2


Instead of setting your list as a model <JSONObject>, you will switch to String, which in this case would be a list of strings.

List<String> categories = new ArrayList<>();

Just below you at first do so, to check if you are listing the data of the returned server structure correctly. See:

String str = "{\"ReportRetryNumber\":4,\"ReportDetailTextList\":[\"alfa\",\"beta\",\"gama\",\"yota\",\"zeta\"]}";

JSONObject jsonObj = new JSONObject(str);
JSONArray array = jsonObj.getJSONArray("ReportDetailTextList");

for(int i = 0; i<array.length(); i++){
    categories.add(array.get(i).toString());
}

To finish, if your JSONParser.getJSONFromUrl( URL ).get("ReportDetailTextList"); really is returning an array, just do it this way below:

JSONArray array = (JSONArray) JSONParser.getJSONFromUrl( URL ).get("ReportDetailTextList");

The complete code within your Try catch should stay this way below::

try{
    List<String> categories = new ArrayList<>();
    JSONArray array = (JSONArray) JSONParser.getJSONFromUrl( URL ).get("ReportDetailTextList");

    for(int i = 0; i<array.length(); i++){
        categories.add(array.get(i).toString());
    }
} catch (JSONException e) {
    e.printStackTrace();
}
  • good did not give no mistake, and turned cute!

  • I’m editing, with the final part.

  • I forgot to tell... to work, I had to comment on the line: dataAdapter = new Arrayadapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, categopries);

  • catechgopries? catechgopries? What to be "catechgopries" ?

  • http://chat.stackexchange.com/rooms/52776/resolvendo-json

Browser other questions tagged

You are not signed in. Login or sign up in order to post.