David is a man subject tolike passion as we are. You should take note that David was going through some molding from God, the great potter. This is because David is to occupy a position that would forver be a reference point in the working of God with man. For such a man that would be occupying this eminent position, David has to be brought into God's potter house. Where he would be molded to have the God image. That is has God's kind of character that is needful for that position.
As a result God allowed the constant deadly pursuit of King Saul and numerous family problems as well as his sexual immorality,they were all working together in building a strong character suitable for the eternal throne of David which Jesus will soon occupy.