David wanted to build the temple of God, but God said no, so why couldn’t David build the Temple of God?
After King Saul had died, it took some time for King David to establish the nation of Israel. It was many years before the land had peace. It was “when the king lived in his house and the LORD had given him rest from all his surrounding enemies, the king said to Nathan the prophet, “See now, I dwell in a house of cedar, but the ark of God dwells in a tent.” And Nathan said to the king, “Go, do all that is in your heart, for the LORD is with you” (2 Sam 7:1-3). It took several years for David to gain peace in the land, but by the time David sat upon the throne, the nation was at its zenith in size and strength. God gave them peace in the land, despite David’s great sin with Bathsheba and having her husband murdered (Uriah). It was because David repented (Psalm 51) and more, that God considered David a man after His own heart (Acts 13:22). So why couldn’t David build the temple of God as he desired? 
Blood Stained Hands
There was nothing evil in what David did in destroying these nations. Many of them practiced pagan religions that included child sacrifice. God wanted these nations out of the Promised Land so that they wouldn’t pollute the true worship of God. If they were allowed to live in the land, the Israelites would eventually adopt their religion, or they’d mix it in with their own worship, but God ensured these nations would be put under subjection by the hand of David. Even though David had blood on his hands, it was God’s will that he be used by God as His sovereign right hand in doing His will. God was executing His righteous judgment through David and the Israelite armies, although we know it was from the hand of God and not from the arm of the flesh that they were delivered (Zech 4:6). The point is, David was in the will of God. Israel was so powerful at that time that most of the nations around them paid tribute to the King. There were no nations in that part of the world that could match Israel’s strength, but the reason for their success wasn’t because of their superior numbers, but because God fought for them. As long as the nation and the king lived in obedience, they had no fear of foreign invasion. David’s rule had brought a great time of peace, but David wanted to do more and build the temple of God, “But that same night the word of the LORD came to Nathan, “Go and tell my servant David, ‘Thus says the LORD: Would you build me a house to dwell in? I have not lived in a house since the day I brought up the people of Israel from Egypt to this day, but I have been moving about in a tent for my dwelling” (2 Sam 7:4-6), so God instructed Nathan the Prophet to tell David, “I will give you rest from all your enemies” (2 Sam 7:11), and “When your days are fulfilled and you lie down with your fathers, I will raise up your offspring after you, who shall come from your body, and I will establish his kingdom. He shall build a house for my name, and I will establish the throne of his kingdom forever” (2 Sam 7:12-13).
The temple of God is often called Solomon’s Temple, but it’s really the temple of God because it was built for God and not Solomon. Solomon only helped to build it, but David already supplied the means to do so, but David told the people of Israel why he couldn’t build the temple. He said, “I had it in my heart to build a house of rest for the ark of the covenant of the LORD and for the footstool of our God, and I made preparations for building. But God said to me, ‘You may not build a house for my name, for you are a man of war and have shed blood” (1 Chron 28:2-3). David had not sinned by shedding blood as it was God’s will that these pagan nations be destroyed, but God said, “It is Solomon your son who shall build my house and my courts, for I have chosen him to be my son, and I will be his father. I will establish his kingdom forever if he continues strong in keeping my commandments and my rules, as he is today” (1 Chron 28:6-7). David may not have had time anyway because it took him decades to gather and prepare the materials and the skilled workers needed to build it. We read in 1 Chronicles 22 where “David commanded to gather together the resident aliens who were in the land of Israel, and he set stonecutters to prepare dressed stones for building the house of God. David also provided great quantities of iron for nails for the doors of the gates and for clamps, as well as bronze in quantities beyond weighing, and cedar timbers without number, for the Sidonians and Tyrians brought great quantities of cedar to David” (1 Chron 22:2-4).
Near the end, “David said, “Solomon my son is young and inexperienced, and the house that is to be built for the LORD must be exceedingly magnificent, of fame and glory throughout all lands. I will therefore make preparation for it.” So David provided materials in great quantity before his death” (1 Chron 22:5), so we can see that it took years to prepare for the building of the temple, but David couldn’t build the temple because of that, but because God said to him, “You may not build a house for my name, for you are a man of war and have shed blood” (1 Chron 28:3). King Solomon would live in a time of peace because of David, and because of David, Solomon was able to complete the temple in His lifetime. David had not sinned because of the blood on his hands, but God was making a point that the temple must be built by those who haven’t shed blood; ironically, the God-Man, Jesus Christ, would come someday to shed His own blood, and then the temple of God would be in the people of God with the Spirit of God.
Here is some related reading for you: Why did Jesus Have to Cleanse the Temple? 
Resource – Scripture quotations are from The Holy Bible, English Standard Version® (ESV®), Crossway Bibles. (2007). ESV: Study Bible: English standard version. Wheaton, Ill: Crossway Bibles. Used by permission. All rights reserved.