Android listview中的自定义适配器有很多ifs

时间:2016-02-17 12:11:10

标签: android if-statement android-arrayadapter

我有listview的适配器,它扩展了基座ArrayAdapter。填充列表成员的整个逻辑是if块。由于只有4个领域,这不是什么大问题。但是,如果你有一个更复杂的adapter怎么办? 为此使用ifs是一个好习惯,还是应该实现更优雅的解决方案(性能明智)?

   if (promo != null) {

        if(!(promo.getItemType().equals(""))){
            holder.item1.setText("Item group :"+ promo.getItemType());
        }
        else if(!(promo.getItemCode().equals(""))){
            holder.item1.setText(promo.getItemCode());
        }

        else if(!(promo.getCustCode().equals(""))){
            holder.item1.setText(promo.getCustCode());
        }
        else if(!(promo.getCustType().equals(""))){
            holder.item1.setText("Clients from: "+ promo.getCustType());
        }

        //set ItemRelation
        if( !(promo.getItemType().equals("")) ){
            holder.item2.setText( promo.getItemCode() );

        }else if(promo.getItemType().equals("1")){
            holder.item2.setText("Group " + promo.getItemCode());

        }else{
            holder.item2.setText("All items");
        }

        if(promo.getPromoType().equals("1")){
            holder.item3.setText(promo.getPercent() + "%");
        }else{
            holder.item3.setText("Sum " + promo.getSalesPrice());
        }

2 个答案:

答案 0 :(得分:1)

Since you haven't shared the code, it would be inappropriate to provide a more elegant solution. However, using a complex adapter doesn't always mean that you will have performance issues. It just has to follow good practices, like using a ViewHolder to help listview recycle the views.

答案 1 :(得分:0)

我不认为有任何特殊的技巧可以避免' ifs'但是使用多个if语句应该没有任何问题。但如果你在if块中使用某种重型循环,那么它会影响你的程序性能