How to display items from JSON file to spinner

chichi :

I am trying to display a list on a spinner from a json file in asset folder. I found a tutorial but I think my format is wrong, please help! also, will this process take time to load to the spinner if the list has up to 300 items?

here is my main activity

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Spinner spinner = (Spinner)findViewById(R.id.spinner);
        ArrayList<String> items = getFood("food.json");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.txt, items);
        spinner.setAdapter(adapter);
    }

    public ArrayList<String> getBanks(String fileName) {
        JSONObject jsonObject = null;

        try {
            InputStream is = getResources().getAssets().open(fileName);
            int size = is.available();
            byte[] data = new byte[size];
            is.read(data);
            is.close();
            String json = new String(data, "UTF-8");
            jsonObject = new JSONObject(json);

            if (jsonObject != null) {
                for (int i = 0; i < jsonObject.length(); i++) {

                  JSONArray foodList = jsonObject.getJSONArray("data");
                    if(cList != null){
                        for(int j=0; i<cList.length(); j++){
                            JSONObject fList = foodList.getJSONObject(j);
                            String food_name = foodList.getString("name");
                            String food_code = foodList.getString("code");

                        }
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException je) {
            je.printStackTrace();
        }
        return cList;
    }
}

the structure of my response, saved as food.json

{
  "status": "Successful",
  "data": [
    {
      "code": "11",
      "id": 1,
      "name": "Apple"
    },
    {
      "code": "22",
      "id": 2,
      "name": "Carrot"
    },
    {
      "code": "33",
      "id": 3,
      "name": "Diamonds"
    },
    {
      "code": " 44",
      "id": 4,
      "name": "Eggs"
    },
   {
      "code": "55",
      "id": 5,
      "name": "Fish"
    }
  ],
  "message": "Action was successful"
}
majuran :

For this task use jackson-jr (Size is very small and faster than other libraries like Gson, Jasckson core ...)

Considering Performance, it is the best option than other.


Now let's go to the solution

First import jackson-jr library to Your android project

implementation 'com.fasterxml.jackson.jr:jackson-jr-objects:2.10.0'

then create two model classes like this

public class Food {
    private String status;
    private List<Data> data;
    private String message;

    public Food() {
    }

    //getters and setters
}

public class Data {
    private String code;
    private int id;
    private String name;

    public Data() {
    }

    //getters and setters
}

Final step get data objects

import com.fasterxml.jackson.jr.ob.JSON;

private List<Data> getData() {
    List<Data> data = null;
    try {
        // give your json file as a inputstream
        //InputStream is = getResources().openRawResource(R.raw.food); 
        InputStream is = getResources().getAssets().open("food.json");
        Food bean = JSON.std.beanFrom(Food.class, is);
        data = bean.getData();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return data;
}

You can do whatever after that using this list of Data...


For passing only food names

There are a lot of ways. Here is a simple way

in your onCreate method use below code

    List<Data> data = getData();
    List<String> foodNames = getFoodNamesFromData(data);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.txt, foodNames);
    // Your usual codes

and here is the getFoodNamesFromData() method

private List<String> getFoodNamesFromData(List<Data> dataList) {
    List<String> foodNames = new ArrayList<>();

    for(Data data :dataList){
        String foodName = data.getName();
        foodNames.add(foodName);
    }

    return foodNames;
}

Happy coding!!!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=335483&siteId=1