Did the events of 1 Samuel 26 and 27 happen close together, or was there a gap or period of time between these events? How could David go from such spiritual growth in Chapter 26 to doubting God's protection in Chapter 27?
The events of Chapters 26 and 27 probably took place a few months or perhaps a year apart. David's spiritual growth evidenced by his respectful behavior toward Saul in Chapter 26 did not eliminate all of David's shortcomings.
David often made decisions out of fear, which compromised his trust in God. For example, in Chapter 27 we read:
1Sam. 27:1 Then David said to himself, “Now I will perish one day by the hand of Saul. There is nothing better for me than to escape into the land of the Philistines. Saul then will despair of searching for me anymore in all the territory of Israel, and I will escape from his hand.”
David "said to himself" that he must flee to the land of the Philistines. This is a clear indication that David was not seeking the counsel of the Lord in this matter but was acting according to his own reasoning. David is a great example when he leans on the Lord, but other times he sets trust aside and responds out of fear.