- setOnClickListener scope on dynamic added elements
- do
- {
- EditText txt1 = new EditText(this);
- EditText txt2 = new EditText(this);
- Button showtxt = new Button(this);
- linearLayout.addView(showtxt );
- linearLayout.addView(txt1);
- linearLayout.addView(txt2);
- showtxt.setOnClickListener(new View.OnClickListener()
- {
- public void onClick(View v)
- {
- String aaa= txt1 .getText().toString();//HOW TO ACCESS txt1 and txt2 from here
- String bbb= txt2 .getText().toString();
- }
- }
- }
- while(somecondition)
- public class Example extends Activity {
- EditText txt1;
- EditText txt2;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- txt1 = new EditText(this);
- txt2 = new EditText(this);
- ...
- do {
- ...
- // EditText[] array = { txt1, txt2 };
- // is the short version of
- EditText[] array = new EditText[2];
- array[0] = txt1;
- array[1] = txt2;
- showtxt.setTag(array);
- showtxt.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- EditText[] array = (EditText[]) v.getTag();
- String aaa = array[0].getText().toString();
- String bbb = array[1].getText().toString();
- Log.v("Example", aaa + " " + bbb);
- }
- });
- } while(some condition)
- View row = new View(this); // or this could be another LinearLayout
- row.setBackgroundColor(0x0000ff);
- // Create and add the Button and EditTexts to row, as in row.addView(showtxt), etc
- ...
- linearLayout.addView(row);
- showtxt.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- View row = v.getParent()
- String aaa = ((EditText) row.getChildAt(1)).getText().toString();
- String bbb = ((EditText) row.getChildAt(2)).getText().toString();
- Log.v("Example", aaa + " " + bbb);
- }
- });