从JSON模型类填充Spinner

嗨,我是Spinner使用的新手,之前从未使用过它,并且还用json数据填充了它。我正在尝试找到一些解决方案,或者至少了解一些有关如何做到这一点的知识,但无法获得任何可以理解的解决方案。如果有人可以引导我走上正确的道路,将不胜感激。

我的JSON数据如下所示,

{ 
 "Devices": [
 {
  "type": "alarm",
  "displayType": "Alarm",
  "imageId": "alarm"
 },
 {
  "type": "audio_bridge",
  "displayType": "Audio Bridge",
  "imageId": "audio"
 },
 {
  "type": "av_receiver",
  "displayType": "Av Receiver",
  "imageId": "default"
 },
 {
  "type": "baby_monitor",
  "displayType": "Baby Monitor",
  "imageId": "mobile"
 },
 {
  "type": "baseport",
  "displayType": "Baseport",
  "imageId": "default"
 },
 {
  "type": "camera",
  "displayType": "Camera",
  "imageId": "camera"
 },
 {
  "type": "console",
  "displayType": "Console",
  "imageId": "console"
 }
 ]
}

我只需要从此JSON中提取displayType。而且我有一个模型类可以跟踪并获取以下所需的任何数据,

@SerializedName("type")
@Expose
private String type;
@SerializedName("displayType")
@Expose
private String displayType;
@SerializedName("imageId")
@Expose
private String imageId;

protected Devices(Parcel in) {
    type = in.readString();
    displayType = in.readString();
    imageId = in.readString();
}

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

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeValue(type);
    parcel.writeValue(displayType);
    parcel.writeValue(imageId);

}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getDisplayType() {
    return displayType;
}

public void setDisplayType(String displayType) {
    this.displayType = displayType;
}

public String getImageId() {
    return imageId;
}

public void setImageId(String imageId) {
    this.imageId = imageId;
}

事情是我想在我的活动中将所有基于json的displayType动态列出到Spinner中。

评论
  • 香思松
    香思松 回复

    您必须创建一个String Arraylist并将其放入微调器适配器中。

    ArrayList<String> deviceTypeList = new ArrayList<String>();
    
    for (device : Devices ){
         deviceTypeList.add (device.getDisplayType())
    }
    
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, deviceTypeList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
    yourSpinnerName.setAdapter (adapter)