1

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.

Rafi Kamal
  • 4,522
  • 8
  • 36
  • 50
  • Can you elaborate a bit more please. but try this - `if(view == null) { view = inflater.inflate(R.layout.game_button, null);}` and let button click listener be re-added. I didn't understand how you use holder[position] = button; as it's a view value which can be recycled by the system so not sure how you intend to use it. – Ashwini Bhangi Apr 29 '13 at 18:22
  • Not related, but your keybord should read HIJK, not HIGK – dberm22 Apr 29 '13 at 19:57
  • @AshwiniBhangi, i've edited my code, now `OnClickListener` is added every time. But now the first item is not getting disabled when I click it. – Rafi Kamal Apr 30 '13 at 06:36
  • @dberm22 Thank you, I haven't noticed it before. – Rafi Kamal Apr 30 '13 at 06:36

1 Answers1

4

Sometimes these things happen becuase you are trying to access a view that has not been inflated yet. Try this:

    View view = convertView;

    if(view == null) 
    {
        view = inflater.inflate(R.layout.game_button, null);
        vto = view.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {@Override public void onGlobalLayout() 
        {
            final Button button = (Button) view.findViewById(R.id.button);
            button.setText(buttonValues[position]);
            holder[position] = button;
            view.setTag(holder[position]);

            ViewTreeObserver obs = view.getViewTreeObserver();
            obs.removeGlobalOnLayoutListener(this);
        }});
    ...

Or possibly put the ViewTreeObserver some place else. It all depends on which side of the if statement the code enters first.

dberm22
  • 3,187
  • 1
  • 31
  • 46