Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Gridview setEnabled不适用于第一项_Android_Android Listview_Android Adapter - Fatal编程技术网

Android Gridview setEnabled不适用于第一项

Android Gridview setEnabled不适用于第一项,android,android-listview,android-adapter,Android,Android Listview,Android Adapter,我今天有个奇怪的问题。我正在尝试实现一个游戏,其中我有一个带有一些按钮的gridview。当单击任何按钮时,它将被禁用(使用setEnabled(false))。问题是,只有第一个按钮没有被禁用。尽管日志输出显示为它调用了setEnabled(false) 这是我的密码: public class ButtonAdapter extends BaseAdapter { private LayoutInflater inflater; private GameScreen gam

我今天有个奇怪的问题。我正在尝试实现一个游戏,其中我有一个带有一些按钮的gridview。当单击任何按钮时,它将被禁用(使用
setEnabled(false)
)。问题是,只有第一个按钮没有被禁用。尽管日志输出显示为它调用了
setEnabled(false)

这是我的密码:

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

我还尝试了
((Button)view.findviewbyd(R.id.Button)).setOnClickListener(…),但没有更改。

有时发生这些情况是因为您试图访问尚未膨胀的视图。试试这个:

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

或者可能将
ViewTreeObserver
放在其他地方。这完全取决于if语句的哪一侧,代码首先进入。

有时这些事情会发生,因为您试图访问尚未膨胀的视图。试试这个:

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

或者可能将
ViewTreeObserver
放在其他地方。这完全取决于if语句的哪一侧,代码首先进入。

请您再详细说明一下。但是如果(view==null){view=inflater.inflate(R.layout.game_button,null)}
并让按钮点击监听器重新添加,请尝试此操作。我不明白你怎么用holder[位置]=按钮;因为它是一个视图值,系统可以循环使用,所以不确定您打算如何使用它。不相关,但您的keybord应该是HIJK,而不是HIGK@AshwiniBhangi,我已经编辑了我的代码,现在每次都会添加
OnClickListener
。但是现在,当我单击第一项时,它并没有被禁用。@dberm22谢谢,我以前没有注意到它。请您再详细说明一下。但是如果(view==null){view=inflater.inflate(R.layout.game_button,null)}并让按钮点击监听器重新添加,请尝试此操作。我不明白你怎么用holder[位置]=按钮;因为它是一个视图值,系统可以循环使用,所以不确定您打算如何使用它。不相关,但您的keybord应该是HIJK,而不是HIGK@AshwiniBhangi,我已经编辑了我的代码,现在每次都会添加
OnClickListener
。但是现在,当我单击第一项时,它没有被禁用。@dberm22谢谢,我以前没有注意到它。