Continuing a chronological Bible study:
(1 Samuel 22:2) And everyone in distress, and everyone who was in debt, and everyone who was discontented, gathered themselves to him, and he became a captain over them, and there were with him about four hundred men.
(1 Chronicles 12:17) And David went out to meet them and answered and said to them, "If you have come peaceably to me to help me, my heart shall be knit to you, but if to betray me to my enemies, seeing there is no wrong in my hands, the God of our fathers look and rebuke."
After David left Gath, he escaped to a cave called Adullam. When they heard he was there, David's family and people from his tribe came to him. It seems that everyone who was discontented with Saul's government in one way or another, also came to David. He went out to meet all the people who had come to him and told them that if they had come to help him, his heart would be as one with theirs, but if they had come to betray him to Saul, since he had done nothing wrong against Saul, David called on God to look upon them and judge. He became a captain over them as they desired to help him against Saul. He gathered about four hundred men.
(18) Then the Spirit came upon Amasai, chief of the captains, "We are yours, David, and on your side, you son of Jesse; peace, peace unto you, and peace to your helpers, for your God helps you. Then David received them and made them captains of the band.
The Holy Spirit came over Amasai, chief of the captains, and he told David that they were all on his side, and he wished peace to David and all who helped him, for he knew it was God who helped him. David then received all the people who had come to him and made them captains of his troop.
(Psalm 142:1) (Maschil of David, a prayer when he was in the cave) I cried to the Lord with my voice; with my voice to the Lord did I make my supplication.
It was at this time while David was dwelling in the cave that he composed this short psalm. The word "maschil" literally meant "instructive"; therefore it seems that David intended this psalm to provide insight and impart wisdom. It may be that David didn't assign that title until sometime after he composed it when he recalled it during times of trouble and saw it as a template of prayer. David cried out to the Lord with his humble request.
(2) I poured out my complaint before Him; I showed before Him my trouble.
David poured out his heart before the Lord, telling Him of all his troubles.
(3) When my spirit was overwhelmed within me, then You knew my path. In the way where I walked have they secretly laid a snare for me.
When David was overwhelmed by his troubles, God knew exactly what was going on, better than David did. He knew that where David went his enemies had laid traps for him.
(4) I looked on my right hand and beheld, but no man who would know me; refuge failed me; no man cared for my soul.
David had previously had servants at his right hand to help him, but at that time there had been no one. His refuge in the cave had failed him because there was no way he could escape and go find help. He felt he had no one who cared to save his life.
(5) I cried to You, O Lord. I said, "You are my refuge, my portion in the land of the living."
David's cave as a refuge had failed him, man had failed him, but he knew that God was his refuge, the only one he could count on in the land of the living.
(6) Attend to my cry for I am brought very low; deliver me from my persecutors for they are stronger than I.
David asked that the Lord hearken to his cry because he had been brought very low in his spirit and in physical things as he was surely hungry, weak, and exhausted. He prayed that the Lord deliver him from those who persecuted him for they were stronger (and much greater in number) than he was.
(7) Bring my soul out of prison that I may praise Your name. The righteous shall compass me about for you shall deal bountifully with me.
David prayed that the Lord bring him out of his prison there holed up in a cave that he might again be able to give God all the glory and praise His name publicly for his deliverance. He had faith that someday the righteous would flock to him and surround him because the Lord had delivered him and blessed him so abundantly. Interestingly, the original word "kathar" that was translated as "compass" also means "to crown." David knew that the Lord had chosen him to be His king, and he had faith that that would come to pass. And of course, the Lord did answer David's prayer and sent many people to the cave to help him.