Matt French
Matt French

Reputation: 25

How to load Json data into a spinner in Android?

I can get the data from the server loaded into a List<>, but can't get the specific items.

Json data looks like this.

[
    {"subscriptionType":"1234,
    "typeName":"stuff",
    "name":"Alpha"},

    {"subscriptionType":"1234,
    "typeName":"stuff",
    "name":"Beta"},
]

and so on...

I have a class that that I load the data into from a Presenter calling a Fetch event. All that seems to be working because I get a Log for the data loaded into the Array

public class AppEntitySubscriptions implements Parcelable {


public AppEntitySubscriptions(ApiSubscription apiSubscription) {

    this.subscriptionType = apiSubscription.getSubscriptionType();
    this.typeName = apiSubscription.getTypeName();
    this.name = apiSubscription.getName();
}


private int subscriptionType;

private String typeName;

private String name;


public

int getSubscriptionType() {
    return subscriptionType;
}

public String getTypeName() {
    return typeName;
}

public String getName() {
    return name;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(this.subscriptionType);
    dest.writeString(this.typeName);
    dest.writeString(this.name);
}

@SuppressWarnings("ResourceType")
protected AppEntitySubscriptions(Parcel in) {
    this.subscriptionType = in.readInt();
    this.typeName = in.readString();
    this.name = in.readString();
}

public static final Creator<AppEntitySubscriptions> CREATOR = new Creator<AppEntitySubscriptions>() {
    @Override
    public AppEntitySubscriptions createFromParcel(Parcel in) {
        return new AppEntitySubscriptions(in);
    }

    @Override
    public AppEntitySubscriptions[] newArray(int size) {
        return new AppEntitySubscriptions[size];
    }
};

Now here is where I am getting lost. I just want to get the data for "name" elements into a spinner

 Spinner spinner = (Spinner) findViewById(R.id.spinner);
 List<AppEntitySubscriptions> userSubscriptions;//data is here
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.toolbar_spinner_item, "what goes here"???);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

I don't know how to get the name into a string array of it's own and loaded into the spinner. Any suggestions would be helpful.

Thanks.

Upvotes: 0

Views: 5514

Answers (1)

Vladimir Markeev
Vladimir Markeev

Reputation: 674

You could not use this list in Spinner

private List<AppEntitySubscriptions> subscriptionsList = new ArrayList<>();

Create new list

private List subscriptionsStrings = new ArrayList<>();

and fill it

for (int i=0; i<subscriptionsList.size(); i++) {
    subscriptionsStrings.add(subscriptionsList.get(i).getTypeName());
}

and then

ArrayAdapter<ArrayList<String>>(this, R.layout.toolbar_spinner_item, subscriptionsStrings);

Upvotes: 2

Related Questions