The background of these verses is David's flight from Absalom. Absalom was determined to become king, so he began undermining David through deceitful and worldly-wise ways: by using psychology, by giving favorable judgments to garner support. In so doing, he began to persuade people to follow him. It took him four years of undermining of his father to get a large enough following to move to overthrow him. He was successful to a point.
Proverbs 28:1 says that the wicked flee when no one pursues. God would certainly not call David a wicked man. He went through periods when he was far from God. Here, he fled for his life into the wilderness east of Jordan. Why did he flee? Wisdom dictated that David was in no position to defend the city: He was outnumbered and "outgunned." Absalom, by his strategy, had gained the upper hand, so David decided that it was better to have freedom of movement in an open place than it was to be trapped in a city, where he would be subject to siege.
So he—like Jesus later on—decided that discretion is the better part of valor. Was David a coward? He was a mighty man of valor, a great man in the eyes of God, and a winner of many military victories. It was a proverb in Israel that "David [has slain] his ten thousands" (I Samuel 18:7; 21:11; 29:5). None would dare call David a coward! At the time, flight was the wisest move; his action was guided by wisdom, not cowardice. Beyond this, David, who usually had his spiritual wits about him, did not presume to discount God's authority to do as He pleased as the real Sovereign of Israel.