0

私のプロジェクトでは、ダイナミックに埋めなければならないいくつかのテキストボックスがあります。私はリストに値を記入する必要があり、リストを反復するときにテキストボックスにsetTextを適用したい。 これは、テキストボックス[1]、テキストボックス[2] ...のような配列型としてウィジェットの名前を持つことは可能ですか?ここではxmlではエラーは出ませんが、読み込み中にアクティビティクラスにエラーがあります、Android XMLのウィジェットの配列を作成してアクティビティを読む

findViewById(R.id.textbox[1] //textbox cannot be resolved or is not a field. 

これを操作する他の方法はありますか?ありがとう。

答えて

2

あなたはリフレクションを使用してみます:

 

for(int i = 0; condition;i++){ 
    try { 
     int id = (Integer) R.id.class.getField("textbox"+i).get(null); 
     TextView textView = (TextView) findViewById(id); 
     textView.setText("i th text from your list"); 
    } catch (IllegalArgumentException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SecurityException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (NoSuchFieldException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
 
+0

ありがとうサンダー、問題解決のおかげで。 – Neetesh

1

だけでなく、あなたがtextboxsの独自の配列を作ることができます...

ArrayList<TextBox> textbox = new new ArrayList<TextBox>(); 
    textbox.add((TextBox)findViewById(R.id.textbox1); 
    textbox.add((TextBox)findViewById(R.id.textbox2); 

その後、好きなことを使用します。テキストボックス[1]

+0

addメソッドTextViewでは動作しません。 plsは細部のコードで精巧です。ありがとう。 – Neetesh

+0

sudarさんの答えが良いかもしれません:) – Amitay

+0

はいamitay、問題はsundarの投稿で解決しました。ウルの助けをありがとう。 – Neetesh

関連する問題