获取在android中循环膨胀的所有edittext值

时间:2012-09-08 07:42:16

标签: android layout-inflater

你好,我是像...一样膨胀的xml文件。

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
        }
    }

现在我的主xml文件中有一个按钮,只需点击该按钮就可以得到上面 _txt2 的所有值。

请帮忙。

修改

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++;
        }
    }

}

错误: -

处的ArrayIndexOutOfBounds
  

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

1 个答案:

答案 0 :(得分:1)

如果_txt2被定义为您的活动类中的一个字段,那么您只需在按钮的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
    }

}

编辑:我没有正确理解你的问题,所以我应该补充一下: 您可以为您创建的任何EditText设置ID。然后在需要时通过findViewById()找到它们。或者您可以将所有EditText存储在EditText数组中并稍后获取它们。