适配器中的Android Retrofit 2问题

时间:2017-01-16 14:32:49

标签: android json adapter retrofit2

我有适配器的问题。我使用改造2来解析JSON数据。下面是JSON示例,模型和适配器代码。

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

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

适配器:

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

我得到&#34;无法解决方法&#34;在getTime()和getValue()。

2 个答案:

答案 0 :(得分:2)

由于getMeasurements()返回List,您显示的JSON只包含一个项目。因此你应该这样称呼它:

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

我建议添加一项检查以查看getMeasurements()是否为空,否则您将获得例外。

答案 1 :(得分:1)

问题是你没有提供任何关于你希望在列表中找到价值或时间的项目的信息。

holder.senzoriMeasurement.setText(curr.getMeasurements().get(<item you wish to get>).getValue;

如果您想获得列表中的第一项

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