如果&其他声明同时执行

时间:2016-04-09 06:12:03

标签: android if-statement

我正在使用 if&否则调用适配器的条件取决于条件。在内部如果我正在调用过滤的数据,以便在ListView&在 Else 内,所有数据都必须列出来。 但在我的情况下,两者都被执行了。

您的回答更受赞赏......

我的代码是:

Restaurant_Beam filterBean = new Restaurant_Beam();
List<Restaurant_Beam> filterRestaurant = new ArrayList<>();
if (Config.startFilterClicked == true) {     /*---Check whether the ToolBar Filter icon is clicked or not*/
    Config.startFilterClicked = false;
    SharedPreferences.Editor editor = CommonUtil.pref.edit();
    editor.putBoolean("startFilter", Config.startFilterClicked);
    editor.commit();

    Restaurant_Beam filter = null;
    for (int i = 0; i < MainActivity.listSuperHeroes.size(); i++) {
        filterBean = MainActivity.listSuperHeroes.get(i);

        int BeanPrice = (int) Math.round(Double.parseDouble(filterBean.getIG_SALES_PRICE()));

        if (Filter_Frgament.strPriceFrom <= BeanPrice && BeanPrice <= Filter_Frgament.strPriceTo) {
            filter = new Restaurant_Beam(filterBean.getIG_PRODUCTID(), filterBean.getIG_PRODUCTNAME(),
                    filterBean.getIG_CATEGORY_ID(), filterBean.getIG_CATEGORYNAME(),
                    filterBean.getIG_SALES_PRICE(), filterBean.getIG_DELIVERY(),
                    filterBean.getIG_IMAGEURL(), filterBean.getIG_VOUCHER_ID(),
                    filterBean.getIG_LIKECOUNT(), filterBean.getIG_VOUCHEROFFER());

            filterRestaurant.add(filter);
            adapter = new CardAdapter(filterRestaurant, context);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            System.out.println("Filter Product Price" + filterBean.getIG_PRODUCTID());
        } else {
            System.out.println("Filter Product Price" + " .***No" + "...1");
        }
    }
} else
    System.out.println("Filter Product Price" + " .***Yes**");
RestaurantDataSet = MainActivity.listSuperHeroes;
for (int i = 0; i < RestaurantDataSet.size(); i++) {
    adapter = new CardAdapter(RestaurantDataSet, context);
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

2 个答案:

答案 0 :(得分:1)

你不会在else块附近有括号

只有打印输出才是其中的一部分。

我相信你打算做什么

else{
System.out.println("Filter Product Price" + " .***Yes**");
RestaurantDataSet =     MainActivity.listSuperHeroes;
for (int i = 0; i < RestaurantDataSet.size(); i++) {
    adapter = new         CardAdapter(RestaurantDataSet, context);
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    }
}

答案 1 :(得分:0)

如果按下过滤器按钮,您似乎只想进行过滤。而且无论如何你想要运行你的“其他”声明?如果是这种情况,您可以使用if语句而不使用else语句,这样您将始终运行代码的后半部分,但只有在按下按钮时才进行过滤。

} else { <---- missing open bracket here
    System.out.println("Filter Product Price" + " .***Yes**");
    RestaurantDataSet = MainActivity.listSuperHeroes;
    for (int i = 0; i < RestaurantDataSet.size(); i++) {
        adapter = new CardAdapter(RestaurantDataSet, context);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
} <---- missing close bracket here