android:使用按钮适配器创建的按钮顺序不正确

时间:2012-09-05 04:04:03

标签: android android-adapter

新到android。我在GridView中创建了12个(3行,4列)按钮,我创建了一个toast,按下按钮按下教程后显示一些内容。按钮正确显示在屏幕上,但吐司消息不正确。当我按左上角按钮时,我得到左下方的吐司。当我按下左中键时,我得到了右中间的吐司。当我按下左下角按钮时,我会获得顶部右侧吐司。

Visually:

button location:
1 2 3 4
5 6 7 8
9 10 11 12

toast message
9 10 11 12
8 7 6 5
4 3 2 1

这是适配器代码:

public class KeypadAdapter extends BaseAdapter {
    private Context mContext;

    // Declare button click listener variable
    private OnClickListener mOnButtonClick;

    // Method to set button click listener variable
    public void setOnButtonClickListener(OnClickListener listener) {
        mOnButtonClick = listener;
    }

    public KeypadAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mButtons.length;
    }

    public Object getItem(int position) {
        return mButtons[position];
    }

    public long getItemId(int position) {
        return 0;
    }

    private KeypadButtons[] mButtons = { KeypadButtons.ADD,
            KeypadButtons.SUBTRACT, KeypadButtons.MULTIPLY,
            KeypadButtons.DIVIDE, KeypadButtons.DET, KeypadButtons.INV,
            KeypadButtons.POW2, KeypadButtons.POWN, KeypadButtons.TRANSPOSE,
            KeypadButtons.NORM1, KeypadButtons.NORM2, KeypadButtons.NORMINF };

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Button btn;
        if (convertView == null) { // if it's not recycled, initialize some
                                    // attributes
            btn = new Button(mContext);
            KeypadButtons keypadButton = mButtons[position];
            if (keypadButton != KeypadButtons.DUMMY) {
                btn.setOnClickListener(mOnButtonClick);
            }

            // Set CalculatorButton enumeration as tag of the button so that we
            // will use this information from our main view to identify what to
            // do
            btn.setTag(keypadButton);
        } else {
            btn = (Button) convertView;
        }

        btn.setText(mButtons[position].getText());
        return btn;
    }

}

这是活动代码

public class MainActivity extends Activity {
    GridView mKeypadGrid;
    KeypadAdapter mKeypadAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get reference to the keypad button GridView
        mKeypadGrid = (GridView) findViewById(R.id.gridView);

        // Create Keypad Adapter
        mKeypadAdapter = new KeypadAdapter(this);

        // Set adapter of the keypad grid
        mKeypadGrid.setAdapter(mKeypadAdapter);

        // Set button click listener of the keypad adapter
        mKeypadAdapter.setOnButtonClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Button btn = (Button) v;
                // Get the KeypadButton value which is used to identify the
                // keypad button from the Button's tag
                KeypadButtons keypadButton = (KeypadButtons) btn.getTag();

                // Process keypad button
                ProcessKeypadInput(keypadButton);
            }
        });

        mKeypadGrid.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

            }
        });

    }

    protected void ProcessKeypadInput(KeypadButtons keypadButton) {
        // TODO Auto-generated method stub
        Toast.makeText(
                MainActivity.this,
                keypadButton.getText().toString() + " "
                        + keypadButton.toString(), Toast.LENGTH_SHORT).show();


    }
}

起初看起来吐司的顺序是相反的。所以我试着做了

KeypadButtons keypadButton = mButtons[mButtons.length - 1 - position];

修复了底部的两行,但他的顶行仍然是颠倒的。

提前致谢。

1 个答案:

答案 0 :(得分:1)

视图被回收(因为您显然知道,因为您正在检查convertview是否为null)。这里一个简单的解决方法是移动btn.setTag(keypadButton); to outside of the if(convertView == null)`块并在从getView()返回之前执行它;