In this chapter, David flees out of Israel and into the land of Gath.
This is the second time that David has fled out of Israel. The first time was back in chapter 21 when David also went to Achish, the king of Gath. In that instance, Achish recognized David as a hero of Israel, so David was afraid and fled back to Israel, to the southern Judean desert. Then he was hunted down and nearly killed multiple times by Saul, and now David seems to have had enough; he is fleeing again.
Achish's response to David is different this time. Perhaps Achish has heard about Saul's many attempts to kill David, so instead of questioning him, Achish allows him to live there. Keep in mind, Achish is still an enemy of Israel. Achish allows David to remain because he believes that David is also an enemy of Saul.
Verses 8-12 are interesting. In this passage, we learn that David spends his time attacking Geshurites, Girzites and Amalekites. These are native Canaanite tribes that are hostile to Israel. The Amalekites in particular are the nation that Israel was sworn to destroy. However, when talking to Achish, David says that he was attacking the Negev (i.e. the southern desert) of Judah, Jerahmeel or the Kenites. The first two are clans of Israel, and the Kenites are a tribe allied with Israel (remember that Moses's father in law was a Kenite). So David is secretly massacring Israel's national enemies, which assuring Achish that he is attacking Israelite (and allied) towns. That's why it says that David left no survivors, because he didn't want any witnesses who could go to Achish and tell him the truth about what David was doing.
This all puts David into an uncomfortable position. Not to say he was comfortable being chased around the Negev by Saul's armies, but now David is hiding amongst the Philistines, pretending to be an enemy of Saul while at the same time attacking Israel's enemies (which are possibly allies of Gath).
Another interesting angle I've heard is that some teachers will assert David was commanded by God to remain in Israel. In 1 Samuel 22:5, the prophet Gad tells David to stay in Judah, and I've heard at least one source assert that this was a "standing order". I.e. because Gad told David to remain in Judah and nobody ever told him to leave, that he was supposed to continue remaining in Judah.
Personally, I don't agree with this position for two reasons. The first reason is that the bible is only telling us a handful of details from the story. So I consider it very possible that Gad (or some other source) had told David that he could leave Judah at some time, or that he should go to Gath, or just anything else. The second reason is that even if nobody had told David to leave Judah, I don't think it's reasonable to expect him to remain there for the rest of his life waiting for somebody to tell him to do something.
In my mind, David should stay in Judah until he accomplishes the purpose for which he was remaining in Judah. But what was that? 1 Samuel 22:5 doesn't tell us, so we are left with speculation. I definitely think it's possible that David waited in Judah long enough.
In summary, I just don't think the text is specific or detailed enough for us to know that this constitutes a violation of what Gad told David to do, but I do think it was worth mentioning.