回收站视图不显示JSON中的数据

时间:2018-09-15 16:46:19

标签: java android json

  

JSON数据= https://www.simplifiedcoding.net/demos/marvel/

这是一个包含3个片段的应用程序(BottomNavigationView的每个选项一个)。在其中一个片段中,我有一个流程,该流程将从JSON文件在线获取数据并将其显示在Recycler视图中。但是我没有任何数据。 我上载了XML文件和logcat报告,以防万一您需要它

ItemOneFragment.java

public class ItemOneFragment extends Fragment {

    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private List<ListItem> listItems;
    private static final String URL_DATA = "https://www.simplifiedcoding.net/demos/marvel/";

    public static ItemOneFragment newInstance() {
        ItemOneFragment fragment = new ItemOneFragment();
        return fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = (RecyclerView) getView().findViewById(R.id.list);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        listItems = new ArrayList<>();

        loadRecyclerViewData();
    }


    private void loadRecyclerViewData(){
        final ProgressDialog progressDialog = new ProgressDialog((this.getActivity()));
        progressDialog.setMessage("Loading Data.....");
        progressDialog.show();

        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_DATA,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {

                        progressDialog.dismiss();
                        try {

                            JSONObject jsonObject = new JSONObject(s);
                            JSONArray array = jsonObject.getJSONArray(s);

                            for (int i = 0;i<array.length();i++){
                              JSONObject o = array.getJSONObject(i);
                              ListItem item = new ListItem(
                                      o.getString("name"),
                                      o.getString("team"),
                                      o.getString("imageurl")
                              );
                              listItems.add(item);
                                Toast.makeText(getActivity().getApplicationContext(), (CharSequence) item,Toast.LENGTH_LONG).show();

                            }

                            adapter = new Adapter(listItems, getActivity().getApplicationContext());

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getActivity().getApplicationContext(),error.getMessage(),Toast.LENGTH_LONG).show();
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this.getActivity());
        requestQueue.add(stringRequest);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        return inflater.inflate(R.layout.frag_one_home, container, false);

    }

}

Adapter.java

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{

    private List<ListItem> listItems;
    private Context context;

    public Adapter(List<ListItem> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
    }

    @Override
    public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_txt,parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(Adapter.ViewHolder holder, int position) {

        ListItem listItem = listItems.get(position);

        holder.textViewTitle.setText(listItem.getTitle());
        holder.textViewStatus.setText(listItem.getStatus());

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView textViewTitle;
        public TextView textViewStatus;
        public ViewHolder(View itemView) {
            super(itemView);
            textViewTitle = (TextView) itemView.findViewById(R.id.place_title);
            textViewStatus = (TextView) itemView.findViewById(R.id.place_description);
        }
    }
}

ListItem.java

public class ListItem {

    private String title;
    private String status;
    private String imageURL;


    public ListItem(String title, String status, String imageURL) {
        this.title = title;
        this.status = status;
        this.imageURL = imageURL;

    }

    public String getTitle() {
        return title;
    }

    public String getStatus() {
        return status;
    }

    public String getImageURL() {
        return imageURL;
    }
}

card_txt.xml

<android.support.v7.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/CardStyle.Home"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    >
<LinearLayout android:id="@+id/text_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
     <!--
        <ImageView
            android:id="@+id/cover_pic"
            android:layout_width="fill_parent"
            android:layout_height="155dp"
            android:contentDescription="@string/todo"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"/>
            -->

        <TextView
            android:id="@+id/place_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="18sp"
             />

        <TextView
            android:id="@+id/place_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="italic"
             />
    </LinearLayout>
</android.support.v7.widget.CardView>

frag_one_home.xml

<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="@dimen/view_spacing_small"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <include layout="@layout/search_bar" />


        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/list"
            />


</LinearLayout>
  

LogCat =   https://raw.githubusercontent.com/teeban10/sandbox/master/log.txt

1 个答案:

答案 0 :(得分:0)

正如日志中指出的那样,由于您没有将this.state = { orderDet: this.props.navigation.state.params.orderDet, numOrder: null, TotalSum: null, }; fetchSum = async () => { const response = await fetch("http://192.168.254.104:3308/OrderNo/ForSum/" + this.state.orderDet) const json = await response.json() this.setState({ TotalSum: json[0].TotalAmountOrdered }) } fetchNumOrders = async () => { const response = await fetch("http://192.168.254.104:3308/NumOrder/num/" + this.state.orderDet ) const json = await response.json() this.setState({ numOrder: json[0].TotalItemsOrdered }) } 设置为Adapter,因此项目没有显示在RecyclerView中,因此它跳过了布局。
您可以这样设置

RecyclerView

接下来,您的JSON数据是对象数组,因此您必须创建adapter = new Adapter(listItems, getActivity().getApplicationContext()); recyclerView.setAdapter(adapter); 而不是JSONArray

JSONObject

最后,您尝试使用//JSONObject jsonObject = new JSONObject(s); JSONArray array = new JSONArray(s); ,但没有在第二个参数中提供Toast,无法将String强制转换为ItemList或{{1 }},只需删除该CharSequence即可解决。