When God looks down at a wicked generation, the rabbis said, He searches for one righteous person to carry the weight of atonement for all the rest. This is the reading Aggadat Bereshit makes of (Ecclesiastes 7:28): "One man among a thousand I have found." God finds, in every generation, the person who will stand in the gap.
Jerusalem was full of wickedness when the prophet Jeremiah received his commission: "Roam the streets of Jerusalem, look around and take note; search her squares and see if you can find a man, one who acts justly and seeks truth, that I might forgive her" (Jeremiah 5:1). The righteous one who could be found would redeem the city. The one who could not be found left the city to its consequences. This is the doctrine of the hidden tzaddik — the unseen righteous person who holds a community together without anyone knowing it.
The man from Ramathaim — Elkanah, father of Samuel — is the figure the midrash points to in this passage. He was among the thousand who did not qualify. But from his house came one who did: Samuel, whom Hannah prayed into existence. The chain from barren woman to prophet to national judge runs through the divine search for one righteous person. God was not looking for a perfect city. He was looking for one person. He found Hannah's prayer, which found Samuel, which found a nation.