0

Hello I am inflate xml file like..

List<EditText> allEds3 = new ArrayList<EditText>();

LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for (int hh1 = 57; hh1 <= 76; hh1++) {
        try {

            View view = inflater.inflate(R.layout.form6, null);
            form6_linear.addView(view);
            lbl1 = (TextView) view.findViewById(R.id.lbl1);
            lbl2 = (TextView) view.findViewById(R.id.lbl2);
            txt1 = (TextView) view.findViewById(R.id.txt1);

            txt1.setId(hh1);
            txt1.setText(txt_array.get(hh1));

            txt1.setOnClickListener(onclicklistener);

            _txt2 = (EditText) view.findViewById(R.id.txt2);

            lbl1.setText(lbl_array.get(hh1));
            lbl2.setText(lbl_array.get(hh1 + 1));

            _txt2.setText(txt_array.get(hh1 + 1));
            allEds3.add(_txt2);

            hh1++;
            nn1++;

        } catch (Exception e) {
            // TODO: handle exception
        }
    }

Now I have one button in my main xml file and onclick of that button I have to get all values of this _txt2 of above.

Please help.

EDIT:

List<EditText> allEds3 = new ArrayList<EditText>();

LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for (int hh1 = 57; hh1 <= 76; hh1++) {
    try {

        View view = inflater.inflate(R.layout.form6, null);
        form6_linear.addView(view);
        lbl1 = (TextView) view.findViewById(R.id.lbl1);
        lbl2 = (TextView) view.findViewById(R.id.lbl2);
        txt1 = (TextView) view.findViewById(R.id.txt1);

        txt1.setId(hh1);
        txt1.setText(txt_array.get(hh1));

        txt1.setOnClickListener(onclicklistener);

        _txt2 = (EditText) view.findViewById(R.id.txt2);

        lbl1.setText(lbl_array.get(hh1));
        lbl2.setText(lbl_array.get(hh1 + 1));

        _txt2.setText(txt_array.get(hh1 + 1));
        allEds3.add(_txt2);

        hh1++;
        nn1++;

    } catch (Exception e) {
        // TODO: handle exception
    }
}

OnClick()

public void onClick(View v) {
switch (v.getId()) {
    case R.id.btn_continue1:
final String[] strings2 = new String[allEds3.size()];

        for (int i = 0; i < allEds3.size(); i++) {
            strings2[i] = allEds3.get(i).getText().toString();
        }

        int ii = 0;
        for (int db = 57; db <= 76; db++) {
            try {
                j.remove(txt_array.get(db));
                j.put(txt_array.get(db), strings2[ii]);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ii++;
        }
    }

}

Error :- ArrayIndexOutOfBounds at

j.put(txt_array.get(db), strings2[ii]);

Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
  • Log db to find out its value when the error occurs. Either you haven't enough members in txt_array or strings2 between position 57..76. – yrajabi Sep 08 '12 at 09:53
  • lbl_array have members between 57 to 76. BUt strings2 have not becaz allEds3 have only one member that i think. – Hardik Joshi Sep 08 '12 at 09:56
  • So log allEds3 size after first "for" loop to see how much large it is. – yrajabi Sep 08 '12 at 10:02
  • 09-08 10:11:22.389: I/allEds3 size-----(1767): 10 IT DISPLAY 10 AFTER FOR LOOP. – Hardik Joshi Sep 08 '12 at 10:11
  • it's because in your first loop when you inflate and create EditText(s), you have increased loop counter inside the loop!!! delete hh1++; at end of the loop, because you have declared it in the loop itself, your loop counter increases twice in each execution, so it loops 10 times instead of 20, and creates only 10 views instead of 20. – yrajabi Sep 08 '12 at 10:16
  • I use hh1++ becase i want to set value of lbl1 to hh1 and lbl2 to hh1+1. Now if i remove it it not give me perfect value in TextView and edittext. – Hardik Joshi Sep 08 '12 at 10:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16438/discussion-between-hardik-joshi-and-yrajabi) – Hardik Joshi Sep 08 '12 at 10:23

1 Answers1

1

If _txt2 is defined as a field in your activity class, then you can simply get it on the button's onClick:

// in onCreate block of your activity
Button button = (Button) findViewById(R.id.button_to_click);
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        // here you can do anything with _txt2 which is just executed when button is clicked
    }

}

EDIT: I didn't figure out your question properly, so I should add this: You can set an ID to any EditText you create. then find them by findViewById() when needed. or you can store all EditText(s) in an array of EditText and get them later.

yrajabi
  • 637
  • 1
  • 9
  • 28
  • But i have 21 edittext. now how to get all edittext string which user entered into onclick method. I am implements onclickListener. – Hardik Joshi Sep 08 '12 at 09:16
  • When user click on button at that time i want to get texts of all edittext not at the time of create. so where i put that code for storing all values into array? – Hardik Joshi Sep 08 '12 at 09:19
  • @hardikjoshi Ok, I edited my answer, use an array to store all 21 EditTexts, and get them on button click. – yrajabi Sep 08 '12 at 09:19
  • lbl_array have members between 57 to 76. BUt strings2 have not becaz allEds3 have only one member that i think. – Hardik Joshi Sep 08 '12 at 09:56
  • What's problem?! I think it was better to keep my answer as "accepted", as your first problem solved. And then ask a new question to continue solving your next problems :) – yrajabi Sep 08 '12 at 12:52