Why Was David Considered A Man After God’s Own Heart?

He walked away from God. He lacked faith. He slept with another man's wife, and he killed a man!