I have a strange problem today. I am trying to implement a game where I have a gridview with some buttons. When any of the buttons are clicked, it get disabled (using setEnabled(false)
). The problem is, only the first button is not getting disabled. Although the log output shows that setEnabled(false)
is called for it.
Here is my code:
public class ButtonAdapter extends BaseAdapter {
private LayoutInflater inflater;
private GameScreen gameScreen;
private Hangman hangman;
private static final String[] buttonValues =
{
"A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U",
"", "V", "W", "X", "Y", "Z", "" ,
};
private Button[] holder = new Button[getCount()];
public ButtonAdapter(GameScreen gameScreen) {
inflater = (LayoutInflater) gameScreen
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.gameScreen = gameScreen;
}
public View getView(final int position, View convertView, ViewGroup arg2) {
View view = convertView;
if(view == null) {
view = inflater.inflate(R.layout.game_button, null);
final Button button = (Button) view.findViewById(R.id.button);
button.setText(buttonValues[position]);
holder[position] = button;
view.setTag(holder[position]);
}
else
holder[position] = (Button) view.getTag();
holder[position].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(buttonValues[position].length() != 0) {
Hangman.GameState gameState = hangman.updateCurrentWord(
buttonValues[position].charAt(0));
holder[position].setEnabled(false);
gameScreen.updateView(gameState);
Log.d("Button", position + ":" + holder[position].getText());
}
}
});
return view;
}
public int getCount() {
return buttonValues.length;
}
public Object getItem(int position) {
return buttonValues[position];
}
public long getItemId(int position) {
return position;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return holder[position].isEnabled();
}
}
I've also tried ((Button) view.findViewById(R.id.button)).setOnClickListener(...);
, but no change.