如何从 json 数组解析 json 数据到 recyclerview java android?

How to parse json data from json array to recyclerview java android?

我可以将 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)

这些行:

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()+"");

您应该以带有占位符的字符串资源的形式将文本传递给文本视图,但这实际上没有任何意义,因为除了不断变化的值之外您没有传递任何东西。

holder.tv_namaEvent.setText(mitem_meeting.get(position).getValue());

您正在尝试设置 Int。 尝试:

String value = String.valueOf(mitem_meeting.get(position).getValue());
holder.tv_namaEvent.setText(value);

请检查所有字段是否为空,并阅读 Android 中的名称约定:)

此外,以 milis 为单位的时间应存储为 Long 类型。 您还可以阅读 Kotlin 语言、mvvn 模式等内容。

谢谢,我得到了 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();
            }
        });

**有一些refactor/rename。