“Create in me a clean heart, O God; and renew a right spirit within me. Cast me not away from thy presence; and take not thy holy spirit from me. Restore unto me the joy of thy salvation; and uphold me with thy free spirit.”(Psalms 51:11-12)
How could a man that lusted after another mans’ wife, committed adultery with her and then had the husband killed be called a "man after God's own heart?" The description of King David is the remarkable answer.
You see, God did not regard David according to the actions of his sinful nature, but according to the deepest motivations and disposition of his heart and will. These were revealed by David's response to the conviction which Nathan the prophet brought upon him; a conviction which brought immediate total confession and repentance, and a broken and contrite heart. This response exemplified the shepherd king's walk with God throughout the entire journey of his life.
Had David made excuses, or rationalized, or resisted the clear message of his sinfulness, the statement would have been, a “man after his own heart.” But the immediacy and completeness of David's confession qualified him for the Lord's forgiveness and mercy, saving him from death and restoring him to that most treasured relationship of his life, his relationship with God.
The consequences of his sin were very serious and followed David all the days of his life. Yet he demonstrated a genuine repentant heart by never railing against God for the troubles which followed him because of his sin with Bathsheba. David always rose up, submitted, and humbly praised God, writing wonderful psalms and pouring out his heart before the Lord. He served God and worshiped Him all his life. So the Lord God didn't reckon David's life by his fall -- but by his walk, and by his heart. Yes, when we come clean, professing our sins longing for forgiveness, through faith in Christ we become God’s own child!