JSON解析数据未显示在Fragment中(使用Volley)

时间:2019-05-11 09:41:45

标签: android json parsing fragment android-volley

几个月前,我开始学习Android开发,目前正在学习处理JSON数据。

几天前,我尝试解析JSON数据并将其显示在RecyclerView中,并且该方法确实有效(就我所关注的整个YouTube教程而言),但是现在我想在Fragment中做同样的事情,不行。

我有一个MainActivity,除了浏览Fragments之外,它目前不执行任何操作。

应用程序正常运行(我可以导航),当我单击SensorsFragment按钮(带有JSON数据的按钮)时,会显示深灰色背景,

我试图找到问题所在,但我不知道它在哪里。

这是我要解析的数据:http://www.mocky.io/v2/5cd67f9d300000630060612b

物品类

public class SensorItem {

    private String mID;
    private String mType;
    private int mValue;

    public SensorItem(String ID, String Type, int value ){
        mID=ID;
        mType=Type;
        mValue=value;
    }

    public String getID(){
        return mID;
    }

    public String getType(){
        return mType;
    }

    public int getValue(){
        return mValue;
    }
}

适配器

public class SensorAdapter extends RecyclerView.Adapter<SensorAdapter.SensorViewHolder> {

    private Context mContext;
    private ArrayList<SensorItem> mSensorList;
    private LayoutInflater inflater;

    public SensorAdapter(Context context, ArrayList<SensorItem> mSensorList){
        inflater = LayoutInflater.from(context);
        this.mContext=context;
        this.mSensorList=mSensorList;

    }

    @Override
    public SensorViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = inflater.inflate(R.layout.sensor_item, viewGroup, false);
        SensorViewHolder holder = new SensorViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(SensorViewHolder holder, int i) {
        SensorItem currentItem = mSensorList.get(i);

        String sensorID = currentItem.getID();
        String sensorType = currentItem.getType();
        int sensorValue = currentItem.getValue();

        holder.mTextViewID.setText(sensorID);
        holder.mTextViewType.setText(sensorType);
        holder.mTextViewValue.setText(sensorValue);
    }

    @Override
    public int getItemCount() {
        return mSensorList.size();
    }

    public void clearAdaptor() {
        mSensorList.clear();
    }

    public class SensorViewHolder extends RecyclerView.ViewHolder{

        public TextView mTextViewID;
        public TextView mTextViewType;
        public TextView mTextViewValue;

        public SensorViewHolder(View itemView) {
            super(itemView);
            mTextViewID = itemView.findViewById(R.id.text_view_id);
            mTextViewType = itemView.findViewById(R.id.text_view_type);
            mTextViewValue = itemView.findViewById(R.id.text_view_value);
        }
    }
}

和片段

public class SensorsFragment extends Fragment {

    private RecyclerView mRyclerView;
    private SensorAdapter mSensorAdapter;
    private ArrayList<SensorItem> mSensorList = new ArrayList<SensorItem>();
    private RequestQueue mRequestQueue;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sensors, container, false);

        mRyclerView = view.findViewById(R.id.recycler_view);

        mSensorAdapter = new SensorAdapter(getContext(), mSensorList);
        mSensorAdapter.clearAdaptor();

        mRyclerView.setAdapter(mSensorAdapter);
        mRyclerView.setHasFixedSize(true);
        mRyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

        mSensorList = new ArrayList<>();
        mRequestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());

        parseJSON();
        return view;
    }

    private void parseJSON(){
        String url = "http://www.mocky.io/v2/5cd67f9d300000630060612b";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("C07-14");

                            for(int i=0; i < jsonArray.length(); i++){
                                JSONObject sensor = jsonArray.getJSONObject(i);

                                String ID = sensor.getString("id");
                                String type = sensor.getString("type");
                                int value = sensor.getInt("value");

                                mSensorList.add(new SensorItem(ID, type, value));
                            }

                            mSensorAdapter.notifyDataSetChanged();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        mRequestQueue.add(request);
    }
}

此外,XML文件:

fragment_sensors.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        />

</RelativeLayout>

sensor_item.xml

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="8dp"
    app:cardCornerRadius="8dp"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp"
        android:gravity="left"
       >

        <TextView
            android:id="@+id/text_view_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ID"
            android:textColor="@android:color/black"
            android:textSize="20sp"/>

        <TextView
            android:id="@+id/text_view_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TYPE"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:layout_below="@+id/text_view_id"/>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp"
        android:gravity="right"
        >

        <TextView
            android:id="@+id/text_view_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="VALUE"
            android:textColor="@android:color/black"
            android:textSize="20sp"/>

    </RelativeLayout>

</android.support.v7.widget.CardView>

1 个答案:

答案 0 :(得分:0)

我已修改您的回复

    try {
        JSONObject jsonObject=new JSONObject(""+response);
        JSONArray jsonArray = jsonObject.getJSONArray("C07-14");

        for(int i=0; i < jsonArray.length(); i++){
            JSONObject sensor = jsonArray.getJSONObject(i);

            String ID = sensor.getString("id");
            String type = sensor.getString("type");
            int value = sensor.getInt("value");

            mSensorList.add(new SensorItem(ID, type, value));
        }

        mSensorAdapter.dataChanged(mSensorList);

    } catch (JSONException e) {
        e.printStackTrace();
    }