从 JSON 模型 class 填充 Spinner

Populate Spinner from JSON model class

嗨,我是微调器的新手,以前从未使用过它,也用 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。我有一个模型 class 来跟踪并获取所需的任何数据,如下所示,

@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 动态地列出到我的 activity 中的微调器中。

您必须创建一个字符串数组列表并将其放入微调器适配器中。

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)

使用自定义适配器传递要在微调器中显示的数据列表,具体取决于要显示的 UI。

public class CustomAdapter extends ArrayAdapter<String>{     

private Activity activity;
private ArrayList data;
public Resources res;
SpinnerModel tempValues=null;
LayoutInflater inflater;

/*************  CustomAdapter Constructor *****************/
public CustomAdapter(
                      CustomSpinner activitySpinner, 
                      int textViewResourceId,   
                      ArrayList objects,
                      Resources resLocal
                     ) 
 {
    super(activitySpinner, textViewResourceId, objects);

    /********** Take passed values **********/
    activity = activitySpinner;
    data     = objects;
    res      = resLocal;

    /***********  Layout inflator to call external xml layout () **********************/
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  }

@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

// This funtion called for each row ( Called data.size() times )
public View getCustomView(int position, View convertView, ViewGroup parent) {

    /********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
    View row = inflater.inflate(R.layout.spinner_rows, parent, false);

    /***** Get each Model object from Arraylist ********/
    tempValues = null;
    tempValues = (SpinnerModel) data.get(position);

    TextView label        = (TextView)row.findViewById(R.id.company);
    TextView sub          = (TextView)row.findViewById(R.id.sub);
    ImageView companyLogo = (ImageView)row.findViewById(R.id.image);

    if(position==0){

        // Default selected Spinner item 
        label.setText("Please select company");
        sub.setText("");
    }
    else
    {
        // Set values for spinner each row 
        label.setText(tempValues.getCompanyName());
        sub.setText(tempValues.getUrl());
        companyLogo.setImageResource(res.getIdentifier
                                     ("com.androidexample.customspinner:drawable/"
                                      + tempValues.getImage(),null,null));

    }   

    return row;
  }

}