【问题标题】:JSON parse data doesn't display in Fragment (using Volley)JSON 解析数据不显示在 Fragment 中(使用 Volley)
【发布时间】:2019-09-29 00:58:26
【问题描述】:

几个月前我开始学习 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>

【问题讨论】:

    标签: android json parsing fragment android-volley


    【解决方案1】:

    我已经编辑了你的回复

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

    【讨论】:

    • 你好,我试过了,没有数据的时候还是一样的显示。我明白了:imgur.com/a/131DW1e
    • 我收到了支票。请记录数组长度
    • like JSONArray jsonArray =jsonObject.getJSONArray("C07-14"); Log.d("arr_len",""+jsonArray.length());
    • 检查我还编辑了你的适配器类和 volley 的 onResponse 方法
    • 还在 AndroidManifest 文件中添加互联网权限
    猜你喜欢
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 2018-06-12
    相关资源
    最近更新 更多