1

我正在尝试编写一个简单的 Boggle 应用程序。有一个“洗牌板”按钮,它应该洗牌所有的立方体并显示新的板。

问题是:即使我更新了LetterAdapter(inside LetterAdapter#shuffle,我调用) 并使用了, 的任何组合,我notifyDataSetChangedrefreshDrawableState()无法让视图被更新。我能找到的唯一工作方法是将适配器重新分配给.invalidate()invalidateViews()onClickGridView

为什么不refreshDrawableState(), invalidate(),invalidateViews()notifyDataSetChanged工作?为什么它们在视觉上没有任何效果?

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final GridView letterGrid = (GridView) findViewById(R.id.letter_grid);
        final LetterAdapter letterAdapter = new LetterAdapter(this);
        letterGrid.setAdapter(letterAdapter);

        Button shuffleButton = (Button) findViewById(R.id.shuffle_button);
        shuffleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                letterAdapter.shuffle();
                // --------------- RELEVANT PART
                // letterGrid.refreshDrawableState();
                // letterGrid.invalidate();
                // letterGrid.invalidateViews();
                letterGrid.setAdapter(letterAdapter);
                // --------------- END
            }
        });
    }

    public class LetterAdapter extends BaseAdapter {
        private Context mContext;
        private BoggleBoard mBoggleBoard;

        public LetterAdapter(Context context) {
            mContext = context;
            mBoggleBoard = new BoggleBoard();
        }

        public void shuffle() {
            mBoggleBoard.shuffle();
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return BoggleBoard.NUM_OF_CUBES;
        }

        @Override
        public Object getItem(int position) {
            return mBoggleBoard.getLetter(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final Button letterButton;
            final String curLetter = mBoggleBoard.getLetter(position);
            if (convertView == null) {
                letterButton = new Button(mContext);
                letterButton.setText(curLetter);
            } else {
                letterButton = (Button) convertView;
            }
            return letterButton;
        }
    }
}

注意:我已经阅读了大约十几个相关的问题/答案,但找不到任何令人满意的答案。

4

1 回答 1

1

问题出在你的getView方法上。您应该letterButton.setText(curLetter);在条件之外调用。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Button letterButton;
    final String curLetter = mBoggleBoard.getLetter(position);
    if (convertView == null) {
        letterButton = new Button(mContext);            
    } else {
        letterButton = (Button) convertView;            
    }

    letterButton.setText(curLetter);        
    return letterButton;
}
于 2013-03-02T15:58:32.907 回答