消除过多if和else语句

时间:2015-07-14 13:52:35

标签: android if-statement filtering

有什么方法可以在我的程序中消除过多if和else语句?这是我的程序

public class SchoolListFragmentFilter extends Fragment implements

    OnItemClickListener {
NavigationDrawer navi = new NavigationDrawer();
String thisFilter[] = NavigationDrawer.theFilter;
int amountTF = NavigationDrawer.TuitionFee;
PSManilaDB conn = new PSManilaDB();
String sList[] = conn.sList();
String sReg[] = conn.sRegAffil();
String sAdmin[] = conn.sAdmin();
String sType[] = conn.sSize();
String sAmbience[] = conn.sAmbience();
String sTuition[] = conn.sTuitionFee();
String sSpecialty[][] = conn.sSpecialty();
int theTuitionFee;

ListView lvList;
ArrayAdapter<String> mAdapter;
ArrayList<String> list = new ArrayList<String>();
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();
ArrayList<Integer> schoolIndex = new ArrayList<Integer>();


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View v = inflater
            .inflate(R.layout.layout_school_list, container, false);
    lvList = (ListView) v.findViewById(R.id.lvSchools);
    for (int loop = 0; loop < sList.length; loop++) {
        String temp = sTuition[loop];
        theTuitionFee = Integer.parseInt(temp);

        // ALL filter selected
        for(int subloop=0; subloop<sSpecialty[loop].length; subloop++) {
        if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
            list.add(sList[loop]);
            // RegAffil
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*") 
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);

            // Administration
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*") 
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);

            // RoomAmbience
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);

            // Tuition Fee
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);

            // Specialty
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
                list.add(sList[loop]);  



            // regAffil and admin 0-1
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // regAffil and type 0-2
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // regaffil and ambience 0-3
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // reg afill and tuition 0-4
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // reg afill and specialty 0-5
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
            list.add(sList[loop]);


            // admin and type 1-2
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // admin and ambience 1-3
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // admin and tuition 1-4
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // admin and specialty 1-5
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
                            list.add(sList[loop]);



            // type and ambience 2-3
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // type and tuition  2-4
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // type and specialty 2-5
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
            list.add(sList[loop]);



            // ambience and tuition 3-4
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // ambience and specialty 3-5
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
            list.add(sList[loop]);

            // tuition and specialty 4-5
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
            list.add(sList[loop]);


            // without specialty

        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")
                ) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);

        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);

        }


        // with specialty
     else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && thisFilter[3].equalsIgnoreCase("*")
            && amountTF == 0
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])
            ) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF == 0
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && thisFilter[3].equalsIgnoreCase("*")
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && thisFilter[1].equalsIgnoreCase("*")
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF == 0
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && thisFilter[1].equalsIgnoreCase("*")
            && thisFilter[3].equalsIgnoreCase("*")
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && thisFilter[1].equalsIgnoreCase("*")
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (thisFilter[0].equalsIgnoreCase("*")
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF == 0
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (thisFilter[0].equalsIgnoreCase("*")
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && thisFilter[3].equalsIgnoreCase("*")
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (thisFilter[0].equalsIgnoreCase("*")
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (thisFilter[0].equalsIgnoreCase("*")
            && thisFilter[1].equalsIgnoreCase("*")
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (thisFilter[0].equalsIgnoreCase("*")
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF == 0
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);

    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && thisFilter[3].equalsIgnoreCase("*")
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    }
    }
    }
    linkedHashSet.addAll(list);
      list.clear();
      list.addAll(linkedHashSet);
    mAdapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, list);
    lvList.setAdapter(mAdapter);
    lvList.setOnItemClickListener(this);

    schoolIndex.clear();
    ArrayList<String> finalFilterList = list;
    ArrayList<String> finalSchoolsList = new ArrayList<String>(
            Arrays.asList(this.sList));
    for (int i = 0; i < finalFilterList.size(); i++) {
        for (int o = 0; o < finalSchoolsList.size(); o++) {
            if (finalFilterList.get(i).equalsIgnoreCase(
                    finalSchoolsList.get(o))) {
                schoolIndex.add(o);
            }
        }
    }
//  Set<String> set = new HashSet<String>(list);

// Then you can create a new ArrayList, this will be a list with no duplicates
//ArrayList<String> newList = new ArrayList<String>(set);
    mAdapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, list);
    lvList.setAdapter(mAdapter);
    lvList.setOnItemClickListener(this);
    if (list.isEmpty()) {
        Toast.makeText(getActivity(), "No Hospitals Found",
                Toast.LENGTH_SHORT).show();
        navi.dia.show();
    }
    return v;
}

现在我可以处理至少5个,并计划添加更多,如果我现在有10个将是300万个组合。

0 个答案:

没有答案