This is a difficult topic. David's story always. He did some pretty terrible things. He didn't respect women. He saw another man's wife bathing and sent soldiers to "take" her and bring her to him so he could sleep with him. At best he was committing adultery at worst rape. He later murdered her husband to cover it up. Not because the King didn't have the power to take a concubine if he wanted one. But because it would have made David look bad to take a married man's wife and exposed his sin to the people. The existence of a harem and multiple wives, as well further adultery, shows that he viewed women as collectors items. It shouldn't be swept under the rug that many concubines of the time were either sold or given into it by their fathers, coerced or forced by either circumstances or their employers, or taken as war captives. Therefore, they had extremely limited to no ability to consent. When David was fleeing from Absalom, he left behind 10 of his concubines to keep house for him, even though he knew soldiers were coming, and sure enough Absalom at the very least degraded them and at best raped them. This disrespect of women is further evident in the way his sons treated the women. Solomon had several hundred wives and concubines. Amnon raped his own sister. Absalom saw his father's concubines as just a means to gain power. Adonijah may or may not have been trying to do the same thing with Abishag. David wasn't the best father, either. When Amnon raped Tamar, David didn't lift a hand to punish Amnon or help Tamar. I get he might have felt he was in no place to judge, or maybe it was just a difficult situation because they were both his children. However, even grounding him would have been better than doing nothing. Then, David banished Absalom when Absalom punished Amnon, which is something David should have done. Later on, David continued to not discipline his kids when Adonijah repeatedly and openly proclaimed himself King and David did nothing until he was on his death bed.
However, David had faith in God and did try to obey the Lord in many things. David showed respect for Saul, God's anointed King, by refusing to rebel against him. He had faith that God would help him kill Goliath. Many of his enemies he punished by making them work as opposed to putting them to death. When the Lord told him to go to war, he went to war. When the Lord told him to hold back, he held back. He showed mercy to members of Saul's family, despite the fact that they could have usurped him. Instead of letting that possibility dictate how he treated them, he trusted that the Lord would determine who reigned.
Ultimately, it was his faith in the Lord that made him a man after God's own heart. And I think David must have tried to do the right thing because no one whose life the Lord has touched and changed can not want to do better. At least he probably wanted to live a better life even if he didn't always succeed. Of course, it wouldn't be good deeds that made him pleasing to the Lord, but his faith. However, "Abraham believed God, and it was credited to him for righteousness" Genesis 15:6. Likewise, Christians today are sanctified through faith in Christ. Because of this, it was most likely David's faith in God that made him a man after God's own heart.