michaelprimez / searchablespinner

Searchable Spinner
Apache License 2.0
314 stars 102 forks source link

How to populate spinner with json array #10

Closed pharma-vie closed 7 years ago

pharma-vie commented 7 years ago

hi Micheal,

My test that does not work.

Classe MainActivity: // Download JSON file AsyncTask new DownloadJSON().execute();

Classe DownloadJSON :

public class DownloadJSON extends AsyncTask<Void, Void, Void> {

    public static ArrayList<String> mStrings = new ArrayList<>();
    public static ArrayList<WorldPopulation> world= new ArrayList<>();
@Override
protected Void doInBackground(Void... params) {
    // JSON file URL address
    jsonobject = JSONfunctions
            .getJSONfromURL("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");
    try {
        // Locate the NodeList name
        jsonarray = jsonobject.getJSONArray("worldpopulation");
        for (int i = 0; i < jsonarray.length(); i++) {
            jsonobject = jsonarray.getJSONObject(i);

            WorldPopulation worldpop = new WorldPopulation();
            worldpop.setCountry(jsonobject.optString("country"));
            world.add(worldpop);

           // Populate spinner with country names
            mStrings.add(jsonobject.optString("country"));
        }
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return null;
}

}
michaelprimez commented 7 years ago

Hi @pharma-vie

Use the onPostExecute of the AsyncTask to notify your adapter [notifyDataSetChanged()].

pharma-vie commented 7 years ago

thiks micheal