Electrocardiogram (ECG) plays an important role in monitoring and preventing heart attacks. In this paper, we propose a new method Adaptive Multilayer Generalized Learning Vector Quantization (AMGLVQ) that integrated feature extraction and classification for the automatic classification of heartbeats in an ECG signal. Since this task has specific characteristics such as, inconsistency optimization on feature extraction and classification, unclassifiable beats and a strong class unbalance, so in this study we proposed new algorithm to handle the problems. The algorithm will be evaluated on real ECG signals from the MIT arrhythmia database. The Experiments show that the proposed method can improve the accuracy of classification better than SVM or back-propagation NN and also able to handle some problems of heartbeat classification: unbalance class, inconsistency between feature extraction and classification and detecting unknown beat on testing phase.