Commands below the setAdapter being executed before getView ()

0

I'm creating some adapters in my application and I've had this doubt in a problem I'm having.

I have a type code:

public class ActivityCompra extends AppCompatActivity {

    Adapter a;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_compra);

        //...

        Thread busca = new Thread();
        busca.execute();
        fazOperacao3();

    }

private class Thread extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute(){
            //...
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            //...
        }

        @Override
        protected void onPostExecute(Boolean ok){
            if(ok){
                a = new Adapter();
                lista.setAdapter(a);
                cancel(true);
                fazOperacao1();
                fazOperacao2();

            }
        }
    }
}

The functions fazOperacao1() , fazOperacao2() and fazOperacao3() are executed before the view of ListView is mounted, and only then getView is called to set the layout components.

I need to perform these operations once the view is already set up and I'm not sure how to do this.

I think it's a very simple thing but I'm having a lot of trouble.

    
asked by anonymous 10.03.2017 / 02:55

1 answer

1

It is strange that it makes Operation 1 and does Operation2 run before the listView is mounted because it is on the bottom line, and in that area the code runs line by line. But, anyway, just use a callback to solve your problems.

public interface MinhaResposta{
    void onResponse(boolean resposta);
}



private class Thread extends AsyncTask<Void, Void, Boolean> {

        private MinhaResposta resposta;

        public Thread(MinhaResposta resposta){
             this.resposta = resposta;
        }

        @Override
        protected void onPreExecute(){
            //...
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            //...
        }

        @Override
        protected void onPostExecute(Boolean ok){
            resposta.onResponse(ok);
        }
    }
}



public class ActivityCompra extends AppCompatActivity {

    Adapter a;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_compra);

        //...

        Thread busca = new Thread(new MinhaResposta(){
              // toda a lógica do onPostExecute
              fazOperacao3(); // aqui vc tem certeza que esse método só será executado após a asynctask ser finalizada
        });
        busca.execute();

    }
    
10.03.2017 / 14:27