Home:ALL Converter>Select editText view contents randomly from stored array

Select editText view contents randomly from stored array

Ask Time:2017-05-04T01:00:41         Author:JamaicanMeCode

Json Formatter

Basically what I am trying to accomplish is storing editText view Strings in an array and returning the value of a randomly selected array element. The editText views can be dynamically added and removed so I had to take that into account. I've gone over my method and cannot find what I'm getting wrong.

the problem is is getAnswer(). Currently this is crashing the app

Java:

public class MainActivity extends AppCompatActivity { private LinearLayout mEditTextContainer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mEditTextContainer = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
    setContentView(activity_main);

    //Button to choose random editText contents
    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             getAnswer();
        }
    });

    //Button to add editText Field
    final Button add_button = (Button) findViewById(R.id.add_button);
    add_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Add_Line();
        }
    });

    //Button to remove editText Field
    final Button remove_button = (Button) findViewById(R.id.remove_button);
    remove_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Remove_Line();
        }
    });
}


public void Add_Line() {
    LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
    // add edittext
    EditText et = new EditText(this);
            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    et.setLayoutParams(p);
    et.setText(null);
    et.setHint("Enter Answer #" + (mEditTextContainer.getChildCount()+1));
    et.setGravity(Gravity.CENTER);
    mEditTextContainer.addView(et);
}

public void Remove_Line() {
    int count = mEditTextContainer.getChildCount();

    if(count == 2)
        return;
    else
        mEditTextContainer.removeViewAt(mEditTextContainer.getChildCount()-1);
}


public void getAnswer() {
    //get number of possible answers
    int count = mEditTextContainer.getChildCount();

    //create array to hold answers
    String[] options = new String[count--];

    //create temporary view to store editText view contents
    View tempView;

    //Loop to collect and store editText contents
    for(int i = 1; i < count-1; i++) {
       tempView = mEditTextContainer.getChildAt(i);
       if(tempView instanceof EditText){
           String tempText = ((EditText) tempView).getText().toString();
                if(tempText != null){
                    options[i] = tempText;
                }
                else
                    return;
       }
    }

    int number = (int)(Math.random() * count-1);
    String answer = options[number];
    TextView answerBox = (TextView)findViewById(R.id.textView7);
    answerBox.setText(answer);
}
}

Author:JamaicanMeCode,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43766029/select-edittext-view-contents-randomly-from-stored-array
yy