“he had turned his heart away.” In the Hebrew text, the verb “turned” is active: it was Solomon who, by his thoughts and actions, turned his heart away from Yahweh. God holds Solomon responsible for his turning away.
“appeared to him twice.” God personally appeared to Solomon on two different occasions (1 Kings 3:5; 9:2), but in spite of his personal and intimate experiences with God, Solomon turned away from Him and did evil in His sight. Solomon is one person who shows us that knowledge does not equal commitment. We can know a lot about God without being committed to him. [For more on God appearing to people, see commentary on Acts 7:55].