2014-08-30 42 views
0

我魔杖禁用一個按鈕(按鈕1和12)這是在 Arraylist通過獲取從Arraylist按鈕1和12。 我已經完成了編碼,但在重新編輯按鈕 arraylist時顯示錯誤... Plz幫助我做到這一點。我想禁用一個按鈕,這是在Arraylist按鈕 - 月蝕爲Android

private ArrayList<Button> b1; 
private Button DisableBtn; 
private Button button1and12; 
private Button button2and9; 
private Button button3and5; 

button1and12 = (Button) findViewById(R.id.button1); 
button2and9 = (Button) findViewById(R.id.button2); 
button3and5 = (Button) findViewById(R.id.button3); 

b1 = new ArrayList<Button>(); 
b1.add(button1and12); 
b1.add(button2and9); 
b1.add(button3and5);   

DisableBtn= b1.get(b1.indexOf(1)); 
DisableBtn.setBackgroundDrawable(R.drawable.image3_5); 
DisableBtn.setEnabled(true); 
+0

請出示從logcat的 – Jens 2014-08-30 07:08:18

+0

堆棧跟蹤好說源沒有發現在礦井線DisableBtn = B1獲得(b1.indexOf(1)); – 2014-08-30 07:36:45

+0

雖然在調試模式下,我發現這個錯誤,但程序運行,但沒有從Arraylist – 2014-08-30 07:38:36

回答

0

假設您已經補充說,按鈕進入名單如下:

b1 = new ArrayList<Button>(); 
b1.add(button1and12); 
b1.add(button2and9); 
b1.add(button3and5); 

到目前爲止沒有錯誤應該出現!現在...

  • 如果要檢索n個按鈕,只要嘗試b1.get(n);
  • 如果要檢索某個按鈕的指標,儘量b1.indexOf(button);
  • 其指數=>b1.remove(n);刪除按鈕
  • 通過其參考刪除按鈕=>b1.remove(button);
  • ...

無論如何,你的indexOf(1)陳述是不正確的,indexOf方法採取一個參數類型Button(在你的情況)而不是int

+0

得到它的工作,已經做了一些其他的錯誤,謝謝你.. – 2014-08-30 14:41:25

0

如果你想使用的indexOf():

DisableBtn= b1.get(b1.indexOf(button1and12)); 

如果你想使用索引:

DisableBtn= b1.get(b1.get(0));