He did this because David had done what he approved and had not disregarded any of his commandments his entire lifetime, except for the incident involving Uriah the Hittite.
Was it not because of things like these that King Solomon of Israel sinned? Among the many nations there was no king like him. He was loved by his God, and God made him king over all Israel. But the foreign wives made even him sin!