在Android中的过滤 – 代码可以运行,但返回屏幕的效果与预期不符。

huangapple 未分类评论44阅读模式
英文:

Filtering in Android - issue that the code works but it doesn't return to the screen as expected

问题

我已经在Android中创建了一个应用程序,用于检索本地地区的餐馆并以列表视图返回它们。我为每个问题中的细节(名称/地址/邮政编码和卫生评分)创建了单独的数组。一些场所是豁免的,数组将以-1的形式返回信息,因此我想使用过滤器进行修改,我认为我已经做到了。

这是我的异步任务(Async Task),它返回JSON数组。

    // 让事情在后台运行,JSON数组来检索列表
    class RetrieveNearestRestaurantsTask extends AsyncTask<Void, Void, JSONArray> {

        private Exception exception;

        // 在后台执行搜索/填充数组列表
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        protected JSONArray doInBackground(Void... voids) {
            // 如果searchUrl转换为空
            if (!searchUrl.toString().isEmpty()) {
                // 进行以下操作
                try {
                    URL url = new URL(searchUrl.toString());
                    URLConnection tc = url.openConnection();
                    InputStreamReader isr = new InputStreamReader(tc.getInputStream());
                    BufferedReader in = new BufferedReader(isr);

                    String line;  // 变量

                    // 当line(读入的行)不等于null时
                    // 创建一个新对象,并将其输出为JSON中的行
                    while ((line = in.readLine()) != null) {
                        ja = new JSONArray(line);

                        // 遍历数组的长度
                        for (int i = 0; i < ja.length(); i++) {
                            JSONObject JO = (JSONObject) ja.get(i);

                            // 输出以满足基本功能要求
                            businessNames.add(JO.getString("BusinessName"));
                            postCodes.add(JO.getString("PostCode"));
                            addressList1.add(JO.getString("AddressLine1"));
                            addressList2.add(JO.getString("AddressLine2"));
                            addressList3.add(JO.getString("AddressLine3"));

                            // 如果餐馆的评分为-1,则显示豁免
                            // 应该显示
                            ratingValue.add(JO.getString("RatingValue"));
                            ratingValue.stream()
                                    .filter(x -> x.equals("-1"))
                                    .findFirst()
                                    .ifPresent(v -> System.out.println("Exempt"));
                            calcDistance.add(JO.getString("DistanceKM"));
                            // 将所有内容一起输出
                            ConcatenateSearchResults();
                        }
                    }
                    isr.close();
                    in.close();
                    //return ja;
                } catch (Exception e) {
                    this.exception = e;

                    return ja;
                } finally {
                    //is.close();
                }
            }
            return ja;
        }

        // 加载搜索结果
        protected void onPostExecute(JSONArray jsonArray) {
                LoadSearchResults();
        }
    }

连接搜索结果:

    private ArrayList<String> ConcatenateSearchResults() {
        int length = businessNames.size();
        ArrayList<String> concatenatedResults = new ArrayList<>();

        if (!businessNames.isEmpty() && !calcDistance.isEmpty() && !ratingValue.isEmpty()
                && !addressList1.isEmpty() && !addressList2.isEmpty() && !addressList3.isEmpty()
                && !postCodes.isEmpty()) {
            for (int i = 0; i < length; i++) {
                concatenatedResults.add("\n" + businessNames.get(i) +
                        "\nDistance (in Km) :" + calcDistance.get(i) +
                        "\n\nRating: " + ratingValue.get(i) +
                        "\nAddress Line 1: " + addressList1.get(i) +
                        "\nAddress Line 2: " + addressList2.get(i) +
                        "\nAddress Line 3: " + addressList3.get(i) +
                        "\nPostcode: " + postCodes.get(i));
            }
        }
        return concatenatedResults;
    }
}

但是,我认为某些地方我没有启用正确的ratingValue变量(?)来返回正确的信息(适用时显示豁免而不是-1),但我没有找出可能出错的地方。感谢任何帮助,我正在慢慢变得更擅长解释我不知道的东西。

英文:

I've created an app in Android to retrieve restaurants in the local area and return them in list view. I've created individual arrays for each of the details in question (name / address / postcode & hygiene rating). Some of the venues are exempt and the array will return the information as -1, so I want to alter this using a filter, which I think I have done.

This is my Async Task which returns the JSON array.

// lets things run in the background, JSON Array to retrieve the list
class RetrieveNearestRestaurantsTask extends AsyncTask&lt;Void, Void, JSONArray&gt; {

    private Exception exception;

    // performs the search in the background / populates the arraylist
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected JSONArray doInBackground(Void... voids) {
        // if the searchUrl conversion is empty
        if (!searchUrl.toString().isEmpty()) {
            // do this instead
            try {
                URL url = new URL(searchUrl.toString());
                URLConnection tc = url.openConnection();
                InputStreamReader isr = new InputStreamReader(tc.getInputStream());
                BufferedReader in = new BufferedReader(isr);

                String line;  // variable

                // while line in (read in) is not equal to null
                // create a new object and output as line in JSON
                while ((line = in.readLine()) != null) {
                    ja = new JSONArray(line);

                    // run through the length of the array

                    for (int i = 0; i &lt; ja.length(); i++) {
                        JSONObject JO = (JSONObject) ja.get(i);

                        // output to meet Basic Functionality requirement
                        businessNames.add(JO.getString(&quot;BusinessName&quot;));
                        postCodes.add(JO.getString(&quot;PostCode&quot;));
                        addressList1.add(JO.getString(&quot;AddressLine1&quot;));
                        addressList2.add(JO.getString(&quot;AddressLine2&quot;));
                        addressList3.add(JO.getString(&quot;AddressLine3&quot;));

                        // if the rating of the restaurant is -1, exempt
                        // should be displayed
                        ratingValue.add(JO.getString(&quot;RatingValue&quot;));
                        ratingValue.stream()
                                .filter(x -&gt; x.equals(&quot;-1&quot;))
                                .findFirst()
                                .ifPresent(v -&gt; System.out.println(&quot;Exempt&quot;));
                        calcDistance.add(JO.getString(&quot;DistanceKM&quot;));
                        // output everything together
                        ConcatenateSearchResults();
                    }
                }
                isr.close();
                in.close();
                //return ja;
            } catch (Exception e) {
                this.exception = e;

                return ja;
            } finally {
                //is.close();
            }
        }
        return ja;
    }

Loads the search results.

    protected void onPostExecute(JSONArray jsonArray) {
            LoadSearchResults();
            }
    }

Concatenates the search results

    private ArrayList&lt;String&gt; ConcatenateSearchResults()
    {
        int length = businessNames.size();
        ArrayList&lt;String&gt; concatenatedResults = new ArrayList&lt;&gt;();

        if(!businessNames.isEmpty() &amp;&amp; !calcDistance.isEmpty() &amp;&amp; !ratingValue.isEmpty()
                &amp;&amp; !addressList1.isEmpty() &amp;&amp; !addressList2.isEmpty() &amp;&amp; !addressList3.isEmpty()
                &amp;&amp; !postCodes.isEmpty())
        {
            for(int i=0; i &lt; length; i++)
            {
                concatenatedResults.add(&quot;\n&quot;+businessNames.get(i) +
                        &quot;\nDistance (in Km) :&quot;+ calcDistance.get(i) +
                        &quot;\n\nRating: &quot;+ ratingValue.get(i) +
                        &quot;\nAddress Line 1: &quot;+ addressList1.get(i) +
                        &quot;\nAddress Line 2: &quot;+ addressList2.get(i)+
                        &quot;\nAddress Line 3: &quot;+ addressList3.get(i) +
                        &quot;\nPostcode: &quot;+ postCodes.get(i));
            }
        }
        return concatenatedResults;
    }
}

However, I think somewhere I haven't enabled the proper ratingValue variable (?) to return the correct information (exempt instead of -1, where applicable, but I haven't worked out what I might have done wrong. Thank you for any help, I am slooowly getting better at explaining what I don't know.

huangapple
  • 本文由 发表于 2020年4月10日 02:13:34
  • 转载请务必保留本文链接:https://java.coder-hub.com/61127583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定