David was a man after God’s own heart … most of the time. His desire to obey and glorify God was the motivating force for his actions throughout the vast majority of his tumultuous life. The obvious exceptions to this are unambiguously called out by the writer of Samuel in words like “But the thing that David had done displeased the Lord” or “God was displeased with this thing, and he struck Israel.”
If you miss those sorts of editorial comments, you are not reading with great care.