2011-06-20 5 views
3

R.javaファイルの静的IDは自動的に生成されますが、私の仕事をより簡単にするためにカスタム値を与えることができます。私は8つのImageボタンを持っています。私は、すべてのボタンにこのコードを使って画像を設定する必要があります。代わりに私が1,2,3にR.javaのボタンのIDを変更... をし、このAndroid R.javaファイル

for(i=0;i<8;i++) 
{ 
ImageButton button4 = (ImageButton)findViewById(i); 
setImagesOnButtons(myContactList.get(3).getPhotoId(),i); 
} 

答えて

1

EDIT:ショーンオーウェンの答えはこれよりもよりよい、よりコンパクトです。

マップを内部値からR.javaの一意のIDに保つことができます。あなただけの起動時に、一度これを実行する必要があります。

static final Map<Integer,Integer> buttonMap = new HashMap<Integer,Integer>(); 

... 
buttonMap.put(4, R.id.iButton4); 
buttonMap.put(3, R.id.iButton3); 
... 

その後、あなたはこのようなあなたのループを持つことができます。

for(i=0;i<8;i++) 
{ 
    ImageButton button = (ImageButton)findViewById(buttonMap.get(i)); 
    setImagesOnButtons(myContactList.get(3).getPhotoId(),i); 
} 
+0

おかげさまで良いアイデア.... –

1

次のようなループのために上記のコードを置くことができ、これを行うの

ImageButton button4 = (ImageButton)findViewById(R.id.iButton4); 
setImagesOnButtons(myContactList.get(3).getPhotoId(),button4); 

ナンバリングに頼ることはできません。手動でR.javaを変更する必要はありません。代わりに、このような何か:

int[] buttonIDs = {R.id.iButton1, R.id.iButton2, ...}; 
for (int i = 0; i < buttonIDs.length; i++) { 
    int buttonID = buttonIDs[i]; 
    ImageButton button4 = (ImageButton) findViewById(buttonID); 
    setImagesOnButtons(myContactList.get(3).getPhotoId(), i); 
} 
+0

おかげで良いアイデア.... –

+0

私は疑問を持っている任意の特定の理由がありますあなたはbuttonIDs [i]を直接使用することができるforループでbuttonIDを使用しました。 –

関連する問題