Reputation: 69
I have problem with adapter. I'm using retrofit 2 to parse JSON data. JSON example, models and adapter code are below.
JSON example:
[{
"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;
}
}
Model Measurement:
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);
}
}
}
I'm getting "Cannot resolve method" on getTime() and getValue().
Upvotes: 0
Views: 231
Reputation: 10778
The problem is you haven't given any information on what item from the List that you wish to find the value or time of.
holder.senzoriMeasurement.setText(curr.getMeasurements().get(<item you wish to get>).getValue;
If you want to get the first item in the List
holder.senzoriMeasurement.setText(curr.getMeasurements().get(0).getValue;
Upvotes: 1
Reputation: 4702
Since getMeasurements()
returns a List
and the JSON you have shown contains only one item. Hence you should call it like this:
holder.senzoriLastAlive.setText(curr.getMeasurements().get(0).getTime());
holder.senzoriMeasurement.setText(curr.getMeasurements().get(0).getValue());
I would suggest adding a check to see if getMeasurements()
is not empty, else you will get an Exception.
Upvotes: 2