Why did he say "Hashem Yadin Amim"?
Rashi: Reverse the judgment from against us, and put it on the Resha'im. "Yadin" is an expression of afflictions.
Radak: "Amim" are Yisrael, like I explained (refer to 7:8:1:2). "Yadin" is to take my vengeance against them. David does not discuss all of them, only the Resha'im who hated David without reason. Most of Yisrael loved David, for he led them [in war] in the days of Sha'ul; they were forced to pursue him, due to Sha'ul's command. Also from Binyamin (Sha'ul's tribe), many loved David, like it says in Divrei ha'Yamim I, 12:1-2; they came to David in Tziklag.
Malbim: When Hashem will judge and inspect the entire Klal, and arrange their deeds opposite mine
Why did he say "Shafteni Hashem k'Tzidki"?
Rashi: Judge Yisrael according to their good deeds, and not based on Aveiros.
Radak: I do not have sin against Yisrael, for which they should hate me.
Malbim: [When Hashem will judge their deeds opposite mine,] He will judge me according to my virtue, for I will be vindicated in my verdict.
What is the difference between "k'Tzidki" and "k'Sumi"?
Malbim (10): Tzedek is external, according to the [visible] deed. Temimus is intent to do Tzedek with a pure heart. Hashem judges nations based on both of these together. Regarding the purity of David's heart, Hashem can judge it by itself, that it is complete with Hashem.