Frage

Hi all I have a problem:

I have a set of 7 EditTexts and I want to check if the user have put information in them and store it in a list. I know I can go with huge number of IF statements but is there a way to do this in a simpler way and not having to write tons of code?

War es hilfreich?

Lösung

Actually there're lots of alternatives. One of them is to use HashMap with appropriate data and TextView:

private HashMap<String, TextView> checkMap = new HashMap<String, TextView>();

public void onCreate(Bundle savedInstanceState){
    checkMap.put("DataA", textViewA);
    checkMap.put("DataB", textViewB);
    //...
}

public boolean checkFields(){
    for (Map.Entry<String, TextView> entry : checkMap.entrySet()) {
        String checkData = entry.getKey();
        TextView textView = entry.getValue();
        if(!textView.getText().toString().equals(checkData))
            return false;
    }

    return true;
}

Other options could be found here: How to avoid a lot of if else conditions

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top