0
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);
            KeypadButton keypadButton = mButtons[position];

            switch(keypadButton.mCategory)
            {
            case MEMORYBUFFER:
                btn.setBackgroundResource(R.drawable.keypadmembuffer1);
                break;  
            case CLEAR:
                btn.setBackgroundResource(R.drawable.keypadclear1);
                break;  
            case NUMBER:
                btn.setBackgroundResource(R.drawable.keypad1);
                break;
            case OPERATOR:

                btn.setBackgroundResource(R.drawable.keypadop1);
                break;
            case OTHER: 
                btn.setBackgroundResource(R.drawable.keypadother1);
                break;
            case RESULT:
                btn.setWidth(8000);
                btn.setBackgroundResource(R.drawable.keypadresult1);
                break;
            case DUMMY:
                btn.setBackgroundResource(R.drawable.appvertical1);
                break;
            default:
                btn.setBackgroundResource(R.drawable.keypad1);
                break;
            }
            // Set OnClickListener of the button to mOnButtonClick
            if(keypadButton != KeypadButton.DUMMY)
                btn.setOnClickListener(mOnButtonClick);
            else
                btn.setClickable(false);
            // 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;
    }

每次我在屏幕上使用滚动时,第一行按钮都会与最后一行按钮混淆。请帮忙 。我是安卓新手。不知道怎么办??我对使用 contextView 的了解非常有限。如果我删除 if else 阻止它可以更正吗?

4

1 回答 1

0

试试这个

 if (convertView == null) { // if it's not recycled, initialize some
                                // attributes

        btn = new Button(mContext);
}
else{
 btn = (Button) convertView;
}
        KeypadButton keypadButton = mButtons[position];

        switch(keypadButton.mCategory)
        {
        case MEMORYBUFFER:
            btn.setBackgroundResource(R.drawable.keypadmembuffer1);
            break;  
        case CLEAR:
            btn.setBackgroundResource(R.drawable.keypadclear1);
            break;  
        case NUMBER:
            btn.setBackgroundResource(R.drawable.keypad1);
            break;
        case OPERATOR:

            btn.setBackgroundResource(R.drawable.keypadop1);
            break;
        case OTHER: 
            btn.setBackgroundResource(R.drawable.keypadother1);
            break;
        case RESULT:
            btn.setWidth(8000);
            btn.setBackgroundResource(R.drawable.keypadresult1);
            break;
        case DUMMY:
            btn.setBackgroundResource(R.drawable.appvertical1);
            break;
        default:
            btn.setBackgroundResource(R.drawable.keypad1);
            break;
        }
        // Set OnClickListener of the button to mOnButtonClick
        if(keypadButton != KeypadButton.DUMMY)
            btn.setOnClickListener(mOnButtonClick);
        else
            btn.setClickable(false);
        // 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);
于 2012-07-04T14:45:57.867 回答