0

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?

user3182266
  • 1,270
  • 4
  • 23
  • 49
  • sure, there is a way. Assign the same handler ( TextChanged or something ) to all of those fields, cast view to EditText, get the text and populate a list, if count of list entries != 7 ( or whatever ), then values are missing. – icbytes May 15 '14 at 13:13

1 Answers1

1

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

Community
  • 1
  • 1
eleven
  • 6,779
  • 2
  • 32
  • 52