'get the value of dynamically generated multiple edittext using gettext in android
I want the text value set on multiple edittext, onclick of button. Edit text is generated dynamically and I want value of each particular edit text. And store that value of text in a string array.
if (type.equalsIgnoreCase("Edittext")) {
Question = cursor1.getString(cursor1
.getColumnIndex(AndroidOpenDbHelper.QUESTIONS));
String ID = cursor1.getString(cursor1
.getColumnIndex(AndroidOpenDbHelper.ID));
// ll_layout1 = (LinearLayout)
// findViewById(R.id.linearlayout1);
tv_edittext = new TextView(this);
tv_edittext.setTextSize(20.0f);
tv_edittext.setText(Question);
ll_layout1.addView(tv_edittext);
et = new EditText(this);
et.setOnClickListener(this);
et.setId(EDITTEXT);
et.setMinLines(1);
et.setMaxLines(3);
// editTextList.add(et);
ll_layout1.addView(et);
// editTextList.add(et);
typelist.add(Question);
}
this code generate Text and edittext. How to start?
Solution 1:[1]
Change it to
tv_edittext = new MyTextView(this);
where MyTextView is :
public class MyTextView extends TextView{
public SampleEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
this.addTextChangedListener(new MyTextWatcher());
}
public SampleEditText(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.addTextChangedListener(new MyTextWatcher());
}
public SampleEditText(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.addTextChangedListener(new MyTextWatcher());
}
}
Use a textwatcher as follows:
public class MyTextWatcher implements TextWatcher {
}
No matter how many textviews you dynamically create, a watcher will be created for it and you can get the text being typed in the onTextChanged method of the textwatcher.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Prithiv Raj |
