Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android Recyclerview中显示JSON数组中包含的JSON对象_Java_Android_Json_Android Recyclerview - Fatal编程技术网

Java 如何在Android Recyclerview中显示JSON数组中包含的JSON对象

Java 如何在Android Recyclerview中显示JSON数组中包含的JSON对象,java,android,json,android-recyclerview,Java,Android,Json,Android Recyclerview,我有以下JSON数据,即一个数组中有10个对象。我在recyclerview中获取对象,没有任何错误 现在我需要做两件事: 我想在用户滚动recyclerview时显示下一页数据,即(接下来的10条记录等等) 我想在recyclerview中显示每个对象的总引号。在显示总引号时,我遇到“空对象引用上的LeadsMeta.getTotalQuotes()”此错误 我已经在onBind-in-Adapter中对该行进行了注释 请帮我解决这两个问题。非常感谢 JSON数据 { "met

我有以下JSON数据,即一个数组中有10个对象。我在recyclerview中获取对象,没有任何错误

现在我需要做两件事:

  • 我想在用户滚动recyclerview时显示下一页数据,即(接下来的10条记录等等)
  • 我想在recyclerview中显示每个对象的总引号。在显示总引号时,我遇到“空对象引用上的LeadsMeta.getTotalQuotes()”此错误
  • 我已经在onBind-in-Adapter中对该行进行了注释

    请帮我解决这两个问题。非常感谢

    JSON数据

    {
       "meta":{
          "code":200,
          "message":"success",
          "total_pages":247,
          "current_page":1,
          "total_items":2469,
          "total_quotes":5
       },
       "data":[
          {
             "id":"4968",
             "topic":"Topic",
             "sub_topic":"Sub Topic",
             "city":"Pune",
             "name":"Shashikant ",
             "quotes":"0",
             "credits":"10",
             "timestamp":"2021-01-05 21:53:38"
          },
          {
             "id":"4963",
             "topic":"Topic",
             "sub_topic":"Sub Topic",
             "city":"Bangalore",
             "name":"Amani",
             "quotes":"0",
             "credits":"10",
             "timestamp":"2021-01-05 20:46:03"
          },
          {
             "id":"4962",
             "topic":"Topic",
             "sub_topic":"Sub Topic",
             "city":"Delhi",
             "name":"Mechanical Engineer",
             "quotes":"0",
             "credits":"10",
             "timestamp":"2021-01-05 20:23:00"
          },
          {
             "id":"4961",
             "topic":"Topic",
             "sub_topic":"Sub Topic",
             "city":"Mumbai",
             "name":"Ankush patil",
             "quotes":"0",
             "credits":"10",
             "timestamp":"2021-01-05 20:20:20"
          },
          {
             "id":"4960",
             "topic":"Topic",
             "sub_topic":"Sub Topic",
             "city":"Delhi",
             "name":"ER Vikash Thakur",
             "quotes":"0",
             "credits":"10",
             "timestamp":"2021-01-05 20:17:32"
          },
          {
             "id":"4957",
             "topic":"Topic",
             "sub_topic":"Sub Topic",
             "city":"Kolkata",
             "name":"Shiladitya Ghosh",
             "quotes":"0",
             "credits":"10",
             "timestamp":"2021-01-05 20:09:44"
          },
          {
             "id":"4956",
             "topic":"Topic",
             "sub_topic":"Sub Topic",
             "city":"Delhi",
             "name":"Vikash",
             "quotes":"0",
             "credits":"10",
             "timestamp":"2021-01-05 20:08:44"
          },
          {
             "id":"4953",
             "topic":"Topic",
             "sub_topic":"Sub Topic",
             "city":"Rishikesh",
             "name":"Rahul",
             "quotes":"0",
             "credits":"10",
             "timestamp":"2021-01-05 19:51:17"
          },
          {
             "id":"4950",
             "topic":"Topic",
             "sub_topic":"Sub Topic",
             "city":"Pune",
             "name":"Abhishek",
             "quotes":"0",
             "credits":"10",
             "timestamp":"2021-01-05 19:43:27"
          },
          {
             "id":"4949",
             "topic":"Topic",
             "sub_topic":"Sub Topic",
             "city":"Chandigarh ",
             "name":"K Singh ",
             "quotes":"0",
             "credits":"10",
             "timestamp":"2021-01-05 19:40:36"
          }
       ]
    }
    
    我的片段:

    public class LeadsFragment extends Fragment {
    
        RecyclerView recyclerView;
        RecyclerView.LayoutManager layoutManager;
        private static final String url = "myurl";
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            View view = inflater.inflate(R.layout.fragment_leads, container, false);
    
            recyclerView = view.findViewById(R.id.recyclerview);
    
            StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
    
                    GsonBuilder gsonBuilder = new GsonBuilder();
                    Gson gson = gsonBuilder.create();
                    LeadModel leadsModelList = gson.fromJson(response, LeadModel.class);
    
                    recyclerView.setHasFixedSize(true);
                    layoutManager = new LinearLayoutManager(getActivity());
                    recyclerView.setLayoutManager(layoutManager);
                    LeadsAdapter leadsAdapter = new LeadsAdapter(getContext(), leadsModelList, recyclerView);
                    recyclerView.setAdapter(leadsAdapter);
    
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
    
                    Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show();
    
                }
            });
    
            RequestQueue queue = Volley.newRequestQueue(getContext());
            queue.add(stringRequest);
    
            return view;
        }
    }
    
    数据类

    public class LeadsData {
    
        @SerializedName("id")
        @Expose
        private String id;
        @SerializedName("topic")
        @Expose
        private String topic;
        @SerializedName("sub_topic")
        @Expose
        private String subTopic;
        @SerializedName("city")
        @Expose
        private String city;
        @SerializedName("name")
        @Expose
        private String name;
        @SerializedName("quotes")
        @Expose
        private String quotes;
        @SerializedName("credits")
        @Expose
        private String credits;
        @SerializedName("timestamp")
        @Expose
        private String timestamp;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getTopic() {
            return topic;
        }
    
        public void setTopic(String topic) {
            this.topic = topic;
        }
    
        public String getSubTopic() {
            return subTopic;
        }
    
        public void setSubTopic(String subTopic) {
            this.subTopic = subTopic;
        }
    
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getQuotes() {
            return quotes;
        }
    
        public void setQuotes(String quotes) {
            this.quotes = quotes;
        }
    
        public String getCredits() {
            return credits;
        }
    
        public void setCredits(String credits) {
            this.credits = credits;
        }
    
        public String getTimestamp() {
            return timestamp;
        }
    
        public void setTimestamp(String timestamp) {
            this.timestamp = timestamp;
        }
    
    }
    
    “meta”对象的SerializedName在LeadModel类中应为“meta”

    public class LeadModel {
         
        //@SerializedName("leadsMeta")
    
        @SerializedName("meta")
        @Expose
        private LeadsMeta leadsMeta;}
    
  • 我想显示下一页数据
  • 请在您的recyclerView上使用
    recyclerView.addOnScrollListener
    。当用户滚动并到达最后一项时,您需要加载更多的10项并将列表附加到现有列表中

    public class LeadsFragment extends Fragment {
    
        RecyclerView recyclerView;
        RecyclerView.LayoutManager layoutManager;
        private static final String url = "myurl";
        boolean isLoading = false; // For Tracking  
    
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ......
        ........
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                }
    
                @Override
                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    
                    if (!isLoading) {
                        if (linearLayoutManager != null && linearLayoutManager.findLastCompletelyVisibleItemPosition() == yourList.size() - 1) {
                            // Last item reached
                            loadMore(); // Note this method
                            isLoading = true;
                        }
                    }
    
                }
            });
    
     private void loadMore() {
            // Make http Request
            // append the new data to LeadsData
            // notifyAdapter items changed
            // set isLoading = false
        }
    
  • 我在一个空对象引用上面临“LeadsMeta.getTotalQuotes()”
  • 这是因为LeadModel中的序列化名称应该与json一致

    @SerializedName("meta")
    

    对于您的第一个问题:

    你需要检查代码。在MainActivity.java类中,有一个方法loadMore()。在您的情况下,您需要执行API调用来加载10个以上的项目,然后通知您的适配器您有新的项目,它将在您的列表中加载它们。这就是你如何用无止境的物品实现你的回收视图

    对于第二个问题:

    您应该将@SerializedName中的leadsMeta更改为meta。 这是您编辑的课程:

    public class LeadModel {
    
        @SerializedName("meta")
        @Expose
        private LeadsMeta leadsMeta;
        @SerializedName("data")
        @Expose
        private List<LeadsData> data = null;
    
        public LeadsMeta getMeta() {
            return leadsMeta;
        }
    
        public void setMeta(LeadsMeta leadsMeta) {
            this.leadsMeta = leadsMeta;
        }
    
        public List<LeadsData> getData() {
            return data;
        }
    
        public void setData(List<LeadsData> data) {
            this.data = data;
        }
    
    }
    
    公共类模型{
    @序列化名称(“元”)
    @暴露
    私人铅塔铅塔;
    @SerializedName(“数据”)
    @暴露
    私有列表数据=null;
    公众领袖梅塔·格梅塔(){
    回程导线数据;
    }
    公共无效设置元(LeadsMeta LeadsMeta){
    this.leadsMeta=leadsMeta;
    }
    公共列表getData(){
    返回数据;
    }
    公共无效设置数据(列表数据){
    这个数据=数据;
    }
    }
    
    我正在分享关于我在回答中分享的代码的教程。快乐编码:)我用@SerializedName(“meta”)的名字犯了一个多么愚蠢的错误。。。谢谢你,做了所有的事。但有一件事还不清楚。如何追加数据并通知适配器//将新数据追加到LeadData//notifyAdapter项已更改
    public class LeadsFragment extends Fragment {
    
        RecyclerView recyclerView;
        RecyclerView.LayoutManager layoutManager;
        private static final String url = "myurl";
        boolean isLoading = false; // For Tracking  
    
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ......
        ........
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                }
    
                @Override
                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    
                    if (!isLoading) {
                        if (linearLayoutManager != null && linearLayoutManager.findLastCompletelyVisibleItemPosition() == yourList.size() - 1) {
                            // Last item reached
                            loadMore(); // Note this method
                            isLoading = true;
                        }
                    }
    
                }
            });
    
     private void loadMore() {
            // Make http Request
            // append the new data to LeadsData
            // notifyAdapter items changed
            // set isLoading = false
        }
    
    @SerializedName("meta")
    
    public class LeadModel {
    
        @SerializedName("meta")
        @Expose
        private LeadsMeta leadsMeta;
        @SerializedName("data")
        @Expose
        private List<LeadsData> data = null;
    
        public LeadsMeta getMeta() {
            return leadsMeta;
        }
    
        public void setMeta(LeadsMeta leadsMeta) {
            this.leadsMeta = leadsMeta;
        }
    
        public List<LeadsData> getData() {
            return data;
        }
    
        public void setData(List<LeadsData> data) {
            this.data = data;
        }
    
    }