Android 改造 2 的问题adapter

标签: json Android
发布时间: 2017/3/4 2:23:55
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有问题, adapter 。我用改造 2 来解析 JSON 数据。JSON 的示例中,模型和 adapter 代码如下。

JSON 的示例︰

[{
"id":1,
"type":"TEMPERATURE",
"measurements":[
    {
        "value":"22.58",
        "time":"2017-01-11T12:20:44.701"
        }]
},{
"id":2,
"type":"HUMIDITY",
"measurements":[
    {
        "value":"52.366",
        "time":"2017-01-11T12:20:44.731"
        }]
},{
"id":3,
"type":"LUMINOSITY",
"measurements":[
    {
        "value":"1.0",
        "time":"2017-01-11T12:20:44.742"
        }]
}]

Senzori 模型︰

public class Senzori {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("type")
@Expose
private String type;
@SerializedName("measurements")
@Expose
private List<Measurement> measurements = new ArrayList<>();

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getType() {
    return type;
}

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

public List<Measurement> getMeasurements() {
    return measurements;
}

public void setMeasurements(List<Measurement> measurements) {
    this.measurements = measurements;
}

}

模型测量︰

public class Measurement {

@SerializedName("value")
@Expose
private String value;
@SerializedName("time")
@Expose
private String time;

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}
}

Adapter:

public class SenzoriAdapter extends RecyclerView.Adapter<SenzoriAdapter.Holder>{

private List<Senzori> mSenzori;

public SenzoriAdapter(){
    mSenzori = new ArrayList<>();
}

@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item, parent, false);
    return new Holder(row);
}

@Override
public void onBindViewHolder(Holder holder, int position) {
    Senzori curr = mSenzori.get(position);
    holder.senzoriType.setText(curr.getType());
    holder.senzoriLastAlive.setText(curr.getMeasurements().getTime());
    holder.senzoriMeasurement.setText(curr.getMeasurements().getValue());

}

@Override
public int getItemCount() {
    return mSenzori.size();
}

public void addSenzori(Senzori senzori) {
    mSenzori.add(senzori);
    notifyDataSetChanged();
}

public class Holder extends RecyclerView.ViewHolder {
    private TextView senzoriType, senzoriLastAlive, senzoriMeasurement;
    public Holder(View itemView) {
        super(itemView);
        senzoriType = (TextView) itemView.findViewById(R.id.senzoriType);
        senzoriLastAlive = (TextView) itemView.findViewById(R.id.senzoriLastAlive);
        senzoriMeasurement = (TextView) itemView.findViewById(R.id.senzoriMeasurement);
    }
}
}

我得到"无法解析法"对 getTime() 和 getValue()。

解决方法 1:

因为 getMeasurements() 返回 List 和你表现出的 JSON 只包含一项。因此你应该这样称呼它︰

holder.senzoriLastAlive.setText(curr.getMeasurements().get(0).getTime());
holder.senzoriMeasurement.setText(curr.getMeasurements().get(0).getValue());

我建议将添加一个检查,检查如果 getMeasurements() 是不是空的否则你会得到一个异常。

赞助商