简体   繁体   English

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

[英]How to parse json data from json array to recyclerview java android?

I can parse json data to my recyclerview.我可以将 json 数据解析到我的 recyclerview。 but when i meet json that has array data in it, my app forceclose when i try to get that.但是当我遇到其中包含数组数据的 json 时,我的应用程序会在我尝试获取该数据时强行关闭。 How to solve this?如何解决这个问题? or anyone has similar sample code with this case?或者任何人都有与这种情况类似的示例代码?

Api: Api:

https://v1.nocodeapi.com/fariz/fit/FunuohzdbtxBkVtC/aggregatesDatasets?dataTypeName=steps_count,heart_minutes,weight,activity_summary https://v1.nocodeapi.com/fariz/fit/FunuohzdbtxBkVtC/aggregatesDatasets?dataTypeName=steps_count,heart_minutes,weight,activity_summary

Activity_Stepcounter.java 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 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 JsonPlaceHolderApi.java

public interface JsonPlaceHolderApi {
   @GET("aggregatesDatasets?dataTypeName=steps_count,heart_minutes,weight,activity_summary")
Call<nodeApi> getNode();}

nodeApi.java 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;
        }
    }
}

error错误

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)

On these lines:在这些方面:

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

Compilator thinks, that you are passing String resource, but you are actually passing simple String.编译器认为,您传递的是字符串资源,但实际上您传递的是简单的字符串。 Just add +"" and it should start working.只需添加 +"" 即可开始工作。

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

You should pass text to textviews in form of String resource with placeholders, but it doesn't really make sense, when you don't pass there anything except the changing value.您应该以带有占位符的 String 资源形式将文本传递给文本视图,但是当您除了更改值之外不传递任何内容时,这实际上没有任何意义。

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

You are trying to set Int.您正在尝试设置 Int。 Try:尝试:

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

Please check all fields for null and read about name convention in Android:)请检查 null 的所有字段并阅读 Android 中的名称约定:)

Also time in milis should be stored in Long type.以 milis 为单位的时间也应存储在 Long 类型中。 You can also read about Kotlin language, mvvn pattern etc.您还可以阅读有关 Kotlin 语言、mvvn 模式等的信息。

Thanks, i got help from discord谢谢,我得到了discord的帮助

I have to make jsonresponse first and then convert it to arraylist using nodeApi.java.我必须先制作 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 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();
            }
        });

**there are some refactor/rename. **有一些重构/重命名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM