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

Evertime I use scroll on my screen first row buttons get mixed up with the last row buttons. Please help . I am new to android . donot know wht to do?? I have a very limited knowledge of using contextView. Can it be corrected if I remove if else block.

1 Answer 1

0

try this

 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);
1
  • @M Moshsin Naeem : Thanks It worked like cheese. Can you please explain a bit how it got corrected like this. I am new to android i had a pretty hard time undestanding this getView and contextview recycling concept.
    – TiMuS
    Jul 4, 2012 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.