【问题标题】:Skipping single item in a sorting method在排序方法中跳过单个项目
【发布时间】:2019-02-09 06:35:47
【问题描述】:

我在将变量排序为费用菜单时遇到问题,我的意思是我有一个 ArrayList,其中的数据为“WITH PEPERONI,1”或“CHAMPAGNE,1,2”,其中 ,1 , 或 ,1,2 表示变量的菜单,因此当我按下菜单 1 时,我只能看到具有 ,1 或 ,1,2 或 ,1,2,3 的变量(其中有 1 ) 在他们的数组中。

实际上我所做的工作但仅适用于具有多个菜单的变量一个菜单为 1 so ,1,在数组中不会被可视化,我不明白为什么。

这是我过滤变量并将它们设置在新数组中的代码:

 public void FilterVariable() {

        filteredVariable = new ArrayList<>();

        for (VariantiConstructor varianti : variantiConstructors) {
            String data = varianti.getMenu();
            String[] items = data.split("," + positionMenu + ",");

            try {
                if (items[0].equals(data)) {
                  //
                } else {
                    filteredVariable.add(varianti);
                }
            } catch (Exception e) {
                //
            }


        }

    }

虽然这里是我正在调试的屏幕,其中有一个 ,2,但它在添加 ArrayList 时跳过了它:

【问题讨论】:

  • 打印异常 logcat 然后继续解决方案
  • 异常没有给出错误
  • 请打印异常,如 e.prinStackTrace();
  • e.printStackTrace();
  • 所以我只需要添加 e.printStackTrace();在捕捉{?因为它没有给出任何结果

标签: android arrays sorting


【解决方案1】:

您需要使用以下代码短接ArrayList

 ArrayList<String> YOUR_ARRAYLIST = new ArrayList<>();

    private void searchDataFromList(String serachString) {


        ArrayList<String> SEARCH_ARRAYLIST = new ArrayList<>();

        for (int i = 0; i < YOUR_ARRAYLIST.size(); i++) {

            if (serachString.contains(YOUR_ARRAYLIST.get(i))) {
                SEARCH_ARRAYLIST.add(YOUR_ARRAYLIST.get(i));
            }

        }
    }

在您的点击监听器上,您需要调用此searchDataFromList() 方法,如下所示

YOUR_CLICK.setOnClickListener(view -> {

    String searchString ="WITH PEPERONI,1,";
    String YOUR_SEARCH_STRING ="";

    List<String> YOUR_SELECETD_LIST = Arrays.asList(searchString.split(","));
    for (int i = 0; i <YOUR_SELECETD_LIST.size(); i++) {
        if (YOUR_SELECETD_LIST.get(i).length()==1)
        {
            YOUR_SEARCH_STRING = YOUR_SELECETD_LIST.get(i);
            System.out.println("VALUE IS ==>>>>> "+YOUR_SEARCH_STRING);
        }
    }

    if (!YOUR_SEARCH_STRING.isEmpty())
    {
        searchDataFromList(YOUR_SEARCH_STRING);
    }

【讨论】:

  • 好的,现在它的工作是相反的,它现在只添加只有一个菜单的项目,所以如果一个变量有菜单,1,另一个,1,2,另一个,1,另一个,1,2,3 , 添加的变量只是一个有 ,1,
  • 也许我只需要在 searchString 中输入不带逗号的菜单号?
  • @JohnKarry ..它对你有用吗..让我知道以防万一
  • 实际上通过反转 YOUR_ARRAYLIST.get(i).getMenu.contains(searchString) 工作谢谢。
  • 所以现在方法是“ filtersVariable = new ArrayList(); for(int i= 0; i
猜你喜欢
  • 2019-05-10
  • 2018-08-24
  • 2012-01-04
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
  • 1970-01-01
相关资源
最近更新 更多