アダプターに問題があります。私はJSONデータを解析するためにretrofit 2を使用しています。 JSONの例、モデル、およびアダプタコードは次のとおりです。アダプターでのAndroid Retrofit 2の問題
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;
}
}
アダプタ:
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()で「メソッドを解決できません」というメッセージが表示されます。
は、私はまだ3分のための答えを受け入れることはできませんが、私は間違いなくそれをやる、それが働いた、ありがとうございました – mustangws