0

I have a JSON sample list as below and I tried to put this into my spinner:-

[{"occupation_id":0,"occupation_name":"Teacher"},{"occupation_id":1,"occupation_name":"Business Owner"}]

When I tried to apply these code:-

val jsonArray = JSONArray(jsonString)

        var list = ArrayList<Occupation>()

        var x = 0
        while (x < jsonArray.length()) {
            var jsonObject = jsonArray.getJSONObject(x)

            list.add(Occupation(
                    jsonObject.getString("occupation_id"),
                    jsonObject.getString("occupation_name")
            ))
            x++
        }

        var spinnerOccupation = findViewById<Spinner>(jasiez.helloworld.jasiez.R.id.spinnerOccupation)
        // Initializing an ArrayAdapter
        val occupationAdapter = ArrayAdapter(
                this, // Context
                android.R.layout.simple_spinner_item,
                list// Array
        )

        occupationAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line)
        spinnerOccupation.adapter = occupationAdapter

I getting this in my spinner

enter image description here

When I tried to change

val occupationAdapter = ArrayAdapter(
                this, // Context
                android.R.layout.simple_spinner_item,
                list// Array
        )

to

val occupationAdapter = ArrayAdapter(
                this, // Context
                android.R.layout.simple_spinner_item,
                list.occupation_name.toList()
        )

I getting error Unresolved reference: occupation_name. If I change to list[0].occupation_name.toList() I will show 1st occupation name with 1 char in every single dropdown list option.

How can I get proper occupation name in each option here? Please help.Thank you.

3 Answers 3

1

When you are using ArrayAdapter, you can only pass the list of String (List), You can't pass like List so make the list like below:

    var list = ArrayList<String>()
    .......
    list.add(jsonObject.getString("occupation_name"))
Sign up to request clarification or add additional context in comments.

Comments

0

I have also encountered the same when dealing with JSon. What I did is I override toString from my Object. In your case, you can override a toString to your occupation name from your Occupations Object.

public class Occupation {
    int occupation_id;
    String occupation_name;

    public Occupation() {
    }

    public int getOccupation_id() {
        return occupation_id;
    }

    public void setOccupation_id(int occupation_id) {
        this.occupation_id = occupation_id;
    }

    public String getOccupation_name() {
        return occupation_name;
    }

    public void setOccupation_name(String occupation_name) {
        this.occupation_name = occupation_name;
    }

    //converts your occupation_name to string
    @NonNull
    @Override
    public String toString() {
        return occupation_name;
        //or you can also add title to your occupation name by doing this
       //return "Occupation: "+ occupation_name;
    }
}

Check out if this works for you.

Comments

0

You can write a custom adapter to hold your data list, and override getview to set the values in spinner's textview. Have a look at this link to see an example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.