1

こんにちはみんな デフォルト値を持つ JSON からいくつかの EditText を動的に作成しました。

EditTexts にいくつかの値を入力した後、ルート ビューのgetChildAt()メソッドを使用してそれらにアクセスしました。EditText の getText()メソッドを使用すると、変更された値ではなく、デフォルト値が返されました。コードで EditText のテキストを設定することさえできませんでした。

これは、EditText を作成するための私の方法です。

private static EditText createEditText(final Context context,
        Element element) {
    final EditText editText = new EditText(context);
    editText.setId(Integer.parseInt(element.getFieldID()));
    editText.setText(element.getCellValue());
    editText.setTag(element);
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            if (event != null
                    && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager inputMethodManager = (InputMethodManager) context
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(
                        editText.getApplicationWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });

    return editText;
}

これは、テキストを変更した後に EditTexts にアクセスする方法です。

public static boolean updateElements(ScrollView rootView) {
    try {
        for (int i = 0; i < rootView.getChildCount(); i++) {
            View view = rootView.getChildAt(i);
            if (view.getClass() == LinearLayout.class) {
                LinearLayout layout = (LinearLayout) view;
                for (int x = 0; x < layout.getChildCount(); x++) {

                    View linearView = layout.getChildAt(x);

                    if (linearView.getClass() == EditText.class) {
                        EditText txtBox = (EditText) linearView;

                        txtBox.setText("TextBox " + x);


                        }
                    }
                }
            }
        }

        return true;

    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }
}

私がここで間違っていることを教えてください...ありがとう。

4

2 に答える 2

1

ついに問題が見つかりました!!!

onCreate( )メソッドとonResume()メソッドの両方でアクティビティのルートビューを設定してきました。また、updateElements(ScrollView rootView)に渡されるビューは、 onCreate()メソッドで作成されたビューです。ただし、実際に表示されるビューは、onResume()で作成されたビューです。

新人の間違い!とにかく助けてくれてありがとう。

于 2012-06-27T06:30:01.430 に答える
0

変更してみる

Object.getClass() == Class.class

Object.getClass().equals(Class.class)
于 2012-06-26T12:17:08.083 に答える