I need help with understanding how the expected value was derived for this question:
I know how to calculate the expected value for when $\theta=0$ and $\theta=1$, but in the solutions, the expected value is $(17-8\theta)p$, but I'm not sure how they got there.

That’s just a convenient way of writing down the answer. It should match your own answer if you did it correctly. To see this, suppose that $\theta=1$. Compare your answer to that given in the solution. Do the same for $\theta=0$.