GridView未更新

时间:2013-03-02 15:43:04

标签: android android-layout android-widget

我正在尝试编写一个简单的Boggle应用程序。有一个“Shuffle Board”按钮,可以随机播放所有立方体并显示新的电路板。

问题是:即使我更新LetterAdapter(在LetterAdapter#shuffle内,我也会调用notifyDataSetChanged)并使用refreshDrawableState()invalidate()的任意组合, invalidateViews()内的onClick我无法让视图更新。我能找到的唯一工作方式是将适配器重新分配给GridView

为什么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;
        }
    }
}

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

1 个答案:

答案 0 :(得分: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;
}