こんにちはみんな デフォルト値を持つ 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;
}
}
私がここで間違っていることを教えてください...ありがとう。