I Kings 15:3 And he walked in all the sins of his father, which he had done before him: and his heart was not perfect with the LORD his God, as the heart of David his father.
David failed God in many ways. The sin against Uriah is specially mentioned in this passage. The interesting thing is that even though David failed in so many ways God said that he had a perfect heart.
What is this perfect heart? We know that David failed. He certainly wasn’t perfect. The key thing is that David in all of his weaknesses loved only One God. He didn’t worship idols. He didn’t go serve them. He stayed faithful to God.
It wasn’t the behavior of David that God noticed nearly as much as the heart David had. David loved God. David believed that God was the only God.
We are not perfect but we can keep an undivided heart towards God. We can love Him and no other gods.