0

こんにちは私は次のような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ファイルにボタンが1つあり、そのボタンをクリックすると、上記の_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 at

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

4

1 に答える 1

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の配列に格納して、後で取得することもできます。

于 2012-09-08T09:10:56.427 に答える