Now I will try to present a better explanation, what were the big wars and bloodshed that prevented David from building the Temple.
But first I will begin with a question. After David explains in the Tanach that he was invalidated from building the Temple because he was a man of wars and spilled much blood on the ground, he does something that seems pretty odd.
He tells his successor, Shlomo {Solomon} who the prophet predicted would build the Temple to arrange for the execution of potential troublemakers, such as Yoav and Shimi the son of Gera [I Melachim/Kings chapter 2].
Shlomo obeys and even goes further. When he sees an indication that his brother, Adoniyahu, has not given up plans of seizing the throne, he also executes Adoniyahu.
Despite all this bloodshed, Shlomo indeed does build the Temple.
And the obvious question, is why, when Shlomo sheds blood is it not an obstacle to building the Temple, but it is for David?
It appears to me, that David asked himself, if wars of Mitzva are fulfilling the will of G-d, what other wars and spilling of blood, could Hashem be enough upset about to cause me to be disqualified from building the Temple.
David concluded, it must be that I had the ability to do some action to stop an unnecessary war or at least it was in my hands to turn a war defeat into a war victory yet I failed to do so.
David at the end of his life concluded, I will at least instruct my son, Shlomo not to repeat my error, by asking him to get rid of the troublemakers at the outset in order to ensure public safety.
Where else do we find that not getting rid of the troublemakers in a timely fashion damages the Temple?
The answer is in tractate Gittin.
In the tractate Gittin pages 55 and 56 we learn that the Caesar of Rome sent an animal to be sacrificed at the Temple and the wicked man, Bar Kamtza physically damaged the animal. It was a type of blemish that invalidates the animal for the sacrificial service by Torah standards, however, according to the standards of the Gentiles, it does not.
The sages of the Sanhedrin thought to eliminate Bar Kamtza who threatened to inform to the Emperor that the Jews were planning a revolt against Rome. The proof being that the Jews didn't agree to sacrifice the Emperor's animal.
At the end they did not kill Bar Kamtza because an elder of the Sanhedrin, Rabbi Zechariah son of Avkulos claimed that one should be pious above the law and not kill Bar Kamtza [who was a Rodef] because perhaps people will wrongfully deduce that Bar Kamtza was killed because he put a blemish on an animal that was consecrated for a sacrifice and not because he was a Rodef.
Concerning the piety of Rabbi Zechariah the Talmud states: "Rabbi Yochanan said 'Onvutanuto' of Rabbi Zechariah son of Avkulos destroyed our Temple burnt the Heichal Building [where the Holy of Holies was located] and exiled us from our land".
Rashi explained there, 'Onvutanuto' - His tolerance that he tolerated this and did not kill him".
And the Meiri commentary there explained: - "And you learn from this that all who have a presumption of striking the community by means of the monarchy, it is permitted to kill him and as they said, "they thought to kill him so he wouldn't go and inform on them to the monarchy. Rabbi Zechariah said to them", etc. Now they said about him, the tolerance that he tolerated this and did not kill him destroyed our Temple".
The obvious question then is what was the unnecessary bloodshed, or defeat did David blame himself for at the end of his life..
I have 2 possible answers, but I will choose to write only about the one, with firm Rabbinical sources.
Namely, had David acted more harshly to Avshalom, {even without killing him} he could have prevented the major civil war that his son Avshalom had instigated. And even though, the war took place after David was already informed that he would not build the Temple, we can say that the seeds of Avshalom's rebellion were already there earlier on.
In what way were the seeds of Avshalom's rebellion already present, when G-d told David he would not build the Temple?
At first I thought to bring tractate Sanhedrin 107a as proof. Afterwards, I found in Midrash Tanchuma at the beginning of Parshat Ki Teizei, an even more explicit proof.
When you go out for war, etc. and you take captives (Dvarim/Deut. 21:10). Our Rabbis taught, a mitzvah brings about (lit. drags along) another mitzvah and a transgression brings about a transgression, And you shall see among the captives a beautiful woman [etc.] and you shall shave her head (there Dvarim/Deut. 21:11,12) in order that she should not find favor in his eyes. What is written afterwards?
If a man has two wives (see there, Dvarim/Deut. 21:15); two women in the house, and there is a quarrel in the house, and furthermore, one of them is beloved and one is hated or both of them are hated. What is written after this? When a man has a stubborn and rebellious son (there, Dvarim/Deut. 21:18). All who marry a beautiful woman (from among the captives), a stubborn and rebellious son will emanate from this relationship. For thus we found with David. Because he desired Maacah the daughter of Talmai, King of Geshur, when he went out for war, Avashalom emanated from him, that sought to kill him, and slept with his wives before all the eyes of Israel and in front of the sun. And through him several tens of thousands of Israel were killed, and he caused a dispute in Israel, and Shimi the son of Gera was killed, and Sheva the son of Bichri, and Achitophel and Mephiboshet, and Ish Boshet, (comment: Eitz Yosef says Ish Boshet should be eliminated from the list, while Mephiboshet was not physically killed but severely economically damaged and I will add put to public shame {see, Orchot Tzaddikim, Shaar Habusha, on the serious of publicly shaming a fellow Jew} and Tziva was made the master over the house of Shaul.
In addition to all this, Midrash Tanchuma to Shmot, also faults David, with giving Avshalom a poor upbringing by not punishing him for evil behavior. By not punishing Avshalom in time, Midrash Tanchuma explains that David is to blame, for Avshalom's evil behavior later on, which included trying to kill David, sleeping with ten of David's wives and causing thousands from Israel to fall (in battle).
Two questions on the above explanation that links David's inability to build the Temple to the episode of Avshalom.
Question 1] Where do we find, that not preventing a crime is made equivalent to some degree to the crime? David was just guilty in not preventing the needless civil war of Avshalom, he himself, did not actively instigate that war.
Answer] We do find that somehow who has the power to prevent evil is somewhat blamed as if he actively did that evil. So for example, the Talmud in tractate Shabbat 56b {Soncino translation} states
But it is written, And Solomon did that which was evil in the sight of the L-rd?( I Kings XI, 6.) — But because he should have restrained his wives, but did not, the Writ regards him as though he sinned.
Question 2] When the Tanach invalidated David from building the Temple, he is called a man of wars [in the plural]. The civil war of Avshalom was only one war.
Answer] Midrash Tanchuma as I already brought above, attributes also the civil war of Sheva Ben Bichri, as being a byproduct of David taking Avshalom's mother as a wife, from among the captives.
Question 3 How can the Rabbis be so critical of marrying a beautiful captive woman, after all the Torah allows it?
Answer Concerning the beautiful captive woman, our Rabbis in tractate Kiddushin 21b, taught (Soncino translation): The Torah only provided for human passions: it is better for Israel to eat flesh of animals] about to die, yet [ritually] slaughtered, than flesh of dying animals which have perished (Soncino comments: Without ritual slaughter. The first too is repulsive, but sanctioned.). End of Quotation
In other words David was expected to realize that this law was meant just for people who are overpowered by their passions and close to rebellion against G-d. It is a leniency not meant to be practiced by the anointed of G-d, who received Divine Inspiration, and authored much of the book of Tehillim/Psalms.