Commands below setAdapter being executed before getView()

Asked

Viewed 51 times

0

I’m creating some Adapters in my app and I’ve come up with this question in a problem I’m having.

I have a code like:

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 ListView be mounted, and only then getView is called to set the layout components.

I need to perform these operations after the view is all set up and I’m not sure how to do it.

I think it’s a very simple thing but I’m having a hard time.

1 answer

1

It is strange to make Peracao1 and Peracao2 be executed before the listview is mounted, because they are on the bottom line, and in that area the code is executed 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();

    }
  • Hello! Sorry for the delay to give a return. I did not understand very well what is happening in your code, could you explain? I need to run Asynktask before doing onPostExecute actions, because I do queries in the database and this data is used in the method

  • asynctask only executes onpostexecute after it is certain that doinbackground has been completed, even if it is network operations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.