I have successfully created EditTexts depending on the user input in Android, and also I have assigned them unique ID's using setId()
method.
Now what I want to do is to get values from the dynamically created EditText
s when the user tap a button, then store all of them in String variables. i.e. value from EditText having id '1' should be saved in str1 of type String, and so on depending on the number of EditTexts.
I am using getid()
, and gettext().toString()
methods but it seems a bit tricky... I cannot assign each value of EditText to a String variable. When I try to do that a NullPointerException
occurs, and if it is not the case where no user input data is shown, I display it in a toast.
Heres, the code:
EditText ed;
for (int i = 0; i < count; i++) {
ed = new EditText(Activity2.this);
ed.setBackgroundResource(R.color.blackOpacity);
ed.setId(id);
ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linear.addView(ed);
}
How do I now pass the value from each EditText to each different string variable? If some body could help with a sample code it would be nice.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…