如何将json数据从json数组解析到recyclerview java android?
Posted
技术标签:
【中文标题】如何将json数据从json数组解析到recyclerview java android?【英文标题】:How to parse json data from json array to recyclerview java android? 【发布时间】:2021-11-19 19:58:06 【问题描述】:我可以将 json 数据解析到我的 recyclerview。但是当我遇到其中包含数组数据的 json 时,当我尝试获取它时,我的应用程序会强制关闭。如何解决这个问题?或者任何人都有与此案例类似的示例代码?
API:
https://v1.nocodeapi.com/fariz/fit/FunuohzdbtxBkVtC/aggregatesDatasets?dataTypeName=steps_count,heart_minutes,weight,activity_summary
Activity_Stepcounter.java
Call<nodeApi> call = jsonPlaceHolderApi.getNode();
call.enqueue(new Callback<nodeApi>()
@Override
public void onResponse(Call<nodeApi> call, Response<nodeApi> response)
progressBar.setVisibility(View.GONE);
nodeApi resource = response.body();
List<nodeApi.steps_count> dataList = resource.steps_count;
Adapter_meeting rv_adapter = new Adapter_meeting(dataList, Activity_Stepcounter.this);
rv.setLayoutManager(new LinearLayoutManager(Activity_Stepcounter.this));
rv.setAdapter(rv_adapter);
rv.hasFixedSize();
rv_adapter.notifyDataSetChanged();
@Override
public void onFailure(Call<nodeApi> call, Throwable t)
call.cancel();
);
Adapter_meeting.java
public class Adapter_meeting extends RecyclerView.Adapter<Adapter_meeting.viewHolder>
private final List<nodeApi.steps_count> mitem_meeting;
private final Context mContext;
public Adapter_meeting(List<nodeApi.steps_count> Item_meeting, Context mContext)
this.mitem_meeting = Item_meeting;
this.mContext = mContext;
@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
final View view;
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_step_counter, parent, false);
final viewHolder vHolder = new viewHolder(view);
return vHolder;
@Override
public void onBindViewHolder(@NonNull viewHolder holder, int position)
holder.tv_namaEvent.setText(mitem_meeting.get(position).getValue());
holder.tv_lokasiEvent.setText(mitem_meeting.get(position).getStartTime());
holder.tv_waktuEvent.setText(mitem_meeting.get(position).getEndTime());
holder.tv_tanggalEvent.setText(mitem_meeting.get(position).getStartTimeMillis());
@Override
public int getItemCount()
return mitem_meeting.size();
public class viewHolder extends RecyclerView.ViewHolder
private final TextView tv_namaEvent;
private final TextView tv_lokasiEvent;
private final TextView tv_waktuEvent;
private final TextView tv_tanggalEvent;
public viewHolder(@NonNull View itemView)
super(itemView);
tv_namaEvent = (TextView) itemView.findViewById(R.id.id_tv_namaEvent);
tv_lokasiEvent = (TextView) itemView.findViewById(R.id.id_tv_lokasiEvent);
tv_waktuEvent = (TextView) itemView.findViewById(R.id.id_tv_waktuEvent);
tv_tanggalEvent = (TextView) itemView.findViewById(R.id.id_tv_tanggalEvent);
JsonPlaceHolderApi.java
public interface JsonPlaceHolderApi
@GET("aggregatesDatasets?dataTypeName=steps_count,heart_minutes,weight,activity_summary")
Call<nodeApi> getNode();
nodeApi.java
public class nodeApi
@SerializedName("steps_count")
public List<steps_count> steps_count = null;
public class steps_count
public Integer value;
public String startTimeMillis;
public String endTimeMillis;
public String startTime;
public String endTime;
public steps_count(Integer value, String startTimeMillis, String endTimeMillis, String startTime, String endTime)
this.value = value;
this.startTimeMillis = startTimeMillis;
this.endTimeMillis = endTimeMillis;
this.startTime = startTime;
this.endTime = endTime;
public Integer getValue()
return value;
public void setValue(Integer value)
this.value = value;
public String getStartTimeMillis()
return startTimeMillis;
public void setStartTimeMillis(String startTimeMillis)
this.startTimeMillis = startTimeMillis;
public String getEndTimeMillis()
return endTimeMillis;
public void setEndTimeMillis(String endTimeMillis)
this.endTimeMillis = endTimeMillis;
public String getStartTime()
return startTime;
public void setStartTime(String startTime)
this.startTime = startTime;
public String getEndTime()
return endTime;
public void setEndTime(String endTime)
this.endTime = endTime;
错误
E/androidRuntime: FATAL EXCEPTION: main
Process: com.iot.testapp, PID: 12849
android.content.res.Resources$NotFoundException: String resource ID #0x19
at android.content.res.Resources.getText(Resources.java:335)
at android.widget.TextView.setText(TextView.java:4555)
at com.iot.testapp.adapter.Adapter_meeting.onBindViewHolder(Adapter_meeting.java:47)
at com.iot.testapp.adapter.Adapter_meeting.onBindViewHolder(Adapter_meeting.java:26)
【问题讨论】:
【参考方案1】:在这些方面:
holder.tv_tanggalEvent.setText(mitem_meeting.get(position).getStartTimeMillis());
holder.tv_lokasiEvent.setText(mitem_meeting.get(position).getStartTime());
holder.tv_waktuEvent.setText(mitem_meeting.get(position).getEndTime());
编译器认为,您传递的是字符串资源,但实际上传递的是简单字符串。只需添加 +"" 即可开始工作。
holder.tv_tanggalEvent.setText(mitem_meeting.get(position).getStartTimeMillis()+"");
holder.tv_namaEvent.setText(mitem_meeting.get(position).getStartTime()+"");
holder.tv_waktuEvent.setText(mitem_meeting.get(position).getEndTime()+"");
您应该将文本以带有占位符的字符串资源的形式传递给 textviews,但是当您除了更改的值之外什么都不传递时,它实际上没有任何意义。
【讨论】:
【参考方案2】:holder.tv_namaEvent.setText(mitem_meeting.get(position).getValue());
您正在尝试设置 Int. 试试:
String value = String.valueOf(mitem_meeting.get(position).getValue());
holder.tv_namaEvent.setText(value);
请检查所有字段是否为 null 并阅读 Android 中的命名约定 :)
Milis 中的时间也应该以 Long 类型存储。 您还可以阅读 Kotlin 语言、mvvn 模式等。
【讨论】:
【参考方案3】:谢谢,我从 discord 那里得到了帮助
我必须先制作 jsonresponse,然后使用 nodeApi.java 将其转换为 arraylist。
public class JsonResponse
private nodeApi[] steps_count;
public nodeApi[] getStep_count()
return steps_count;
public void setnodeApi(nodeApi[] step_count)
this.steps_count = step_count;
Activity_Stepcounter.java
Call<JsonResponse> call = jsonPlaceHolderApi.getStep();
call.enqueue(new Callback<JsonResponse>()
@Override
public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response)
progressBar.setVisibility(View.GONE);
JsonResponse jsonResponse = response.body();
listStep = new ArrayList<>(Arrays.asList(jsonResponse.getStep_count()));
Adapter_meeting rv_adapter = new Adapter_meeting(listStep, Activity_Stepcounter.this);
rv.setLayoutManager(new LinearLayoutManager(Activity_Stepcounter.this));
rv.setAdapter(rv_adapter);
rv.hasFixedSize();
rv_adapter.notifyDataSetChanged();
@Override
public void onFailure(Call<JsonResponse> call, Throwable t)
call.cancel();
);
**有一些重构/重命名。
【讨论】:
以上是关于如何将json数据从json数组解析到recyclerview java android?的主要内容,如果未能解决你的问题,请参考以下文章