【问题标题】:How do I print a 10 first items on a Array List如何在数组列表上打印 10 个第一个项目
【发布时间】:2017-10-04 23:37:24
【问题描述】:

我的 ArrayList 有 100 行,但我只想显示前 10 个条目。也许有人会知道怎么做?

try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                JSONArray devices = jsonObj.getJSONArray("list");

                for (int i = 0; i < devices.length(); i++) {
                    JSONObject c = devices.getJSONObject(i);
                    String id = c.getString("id");
                    String name = c.getString("name");
                    String known = c.getString("known");
                    String description = c.getString("description");
                    String controllerId = c.getString("controllerId");


                    JSONObject frequencySurvey = c.getJSONObject("frequencySurvey");
                    if (frequencySurvey.has("5000")) {
                        JSONObject fivezero = frequencySurvey.getJSONObject("5000");

                        String timestamp = fivezero.getString("timestamp");
                        String clients = fivezero.getString("clients");
                        String enabled = fivezero.getString("enabled");

                        HashMap<String, String> device = new HashMap<>();

                        device.put("id", id);
                        device.put("name", name);
                        device.put("enabled", enabled);
                        device.put("clients", clients);

                        DevicesList.add(device);

                    }
                }
            }

ListAdapter adapter = new SimpleAdapter(Devices_5_0.this, DevicesList,
                R.layout.list_item, new String[]{ "name","clients","enabled"},
                new int[]{R.id.name,R.id.clients, enabled});

        lv.setAdapter(adapter);

创建时:

DevicesList = new ArrayList<>();

全局声明:

    ArrayList<HashMap<String, String>> DevicesList;

我绝对不能提前限制我的清单

【问题讨论】:

    标签: android sorting arraylist hashmap


    【解决方案1】:

    如果您的devices JSONArray 大小大于10,则仅针对first 10 条目解析JSONObject。

    试试这个:

           try {
                JSONObject jsonObj = new JSONObject(jsonStr);
    
                JSONArray devices = jsonObj.getJSONArray("list");
    
                int limit = devices.length();
                if (limit > 10) 
                    limit = 10;
    
                for (int i = 0; i < limit; i++) {
                    JSONObject c = devices.getJSONObject(i);
                    String id = c.getString("id");
                    String name = c.getString("name");
                    String known = c.getString("known");
                    String description = c.getString("description");
                    String controllerId = c.getString("controllerId");
    
    
                    JSONObject frequencySurvey = c.getJSONObject("frequencySurvey");
                    if (frequencySurvey.has("5000")) {
                        JSONObject fivezero = frequencySurvey.getJSONObject("5000");
    
                        String timestamp = fivezero.getString("timestamp");
                        String clients = fivezero.getString("clients");
                        String enabled = fivezero.getString("enabled");
    
                        HashMap<String, String> device = new HashMap<>();
    
                        device.put("id", id);
                        device.put("name", name);
                        device.put("enabled", enabled);
                        device.put("clients", clients);
    
                        DevicesList.add(device);
    
                    }
                }
            }
    

    希望对你有帮助~

    【讨论】:

      【解决方案2】:

      你像这样添加一个for循环:

      for(int i = 0; i < (yourArray.size() <= 10 ? yourArray.size() : 10); i++){
          //Add to another list, or do something to store these entries
      }
      
      //Display them, print them, whatever you need
      

      它最多打印 10 个第一个条目。如果数组的大小

      【讨论】:

      • 我建议Math.min(10, yourArray.size()) 稍微提高可读性。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      • 2017-09-24
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      相关资源
      最近更新 更多