如何在Android中将Json数据加载到微调器中?

时间:2015-11-12 02:42:58

标签: android arrays json spinner

我可以将服务器中的数据加载到List<>中,但无法获取特定项目。

Json数据看起来像这样。

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

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

依旧......

我有一个类,我从调用Fetch事件的Presenter加载数据。所有这一切似乎都有效,因为我得到了加载到数组中的数据的日志

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];
    }
};

现在这里是我迷路的地方。我只是想将“name”元素的数据转换为微调器

 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);

我不知道如何将名称放入它自己的字符串数组中并加载到微调器中。任何建议都会有所帮助。

感谢。

1 个答案:

答案 0 :(得分:1)

您无法在Spinner

中使用此列表
private List<AppEntitySubscriptions> subscriptionsList = new ArrayList<>();

创建新列表

private List subscriptionsStrings = new ArrayList<>();

并填写

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

然后

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