Magnesium metal forms a doubly charged cation. Iodide forms a singly charged anion.
##Mg^(2+) + 2I^(-) rarr MgI_2##
How do you know what ions these form? Look at . Magnesium comes from Group 2; as a metal it needs to lose 2 electrons to assume a Noble Gas configuration (that of neon); on the other hand iodine needs to gain 1 electron to assume the xenon configuration. The ion formation reflects this tendency.
What is the formula of potassium oxide?