I want to disable a button that is inside the Arraylist button - eclipse for Android

advertisements

I wand to disable a button called(button1and12) which is inside Arraylist by fetching the button1and12 from the Arraylist. I have done the coding but shows error in reterving the button from arraylist...Plz help me out to do it.

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);


Suppose you've added that buttons into the list like this:

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

So far no errors should appear! Now ...

  • If you want to retrieve nth button, just try b1.get(n);
  • If you want to retrieve index of a certain button, try b1.indexOf(button);
  • Remove a button by its index => b1.remove(n);
  • Remove a button by its reference => b1.remove(button);
  • ...

Anyway, your indexOf(1) statement is not correct, indexOf method take one argument with type Button (in your case) not int .