user3238711 user3238711 - 11 months ago 43
Android Question

How to implement AsyncTask in my code.?

I am a beginner in android and programming in general and I have a personal project relating to my work, which is basically a set of queries to an existing database.

One of the queries takes too long and I need to implement the method asyntask and put a loading in screen. I've read various examples(complexes and complicates) but I can not implement in my project, so I come here to ask the community for help, to guide me in the correct placement of the code

In short, this is my code

This is my main activity, with a button click event and two edittext two search.

Button button = (Button)vendas.findViewById(;
Button button1 = (Button)vendas.findViewById(;
button.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

Intent intent = new Intent(getActivity(), VendasActivity.class);
EditText editText = (EditText) vendas.findViewById(;
EditText editText1 = (EditText) vendas.findViewById(;
String message = editText.getText().toString();
String message1 = editText1.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
intent.putExtra(EXTRA_MESSAGE1, message1);

my VendasActivity.class where I have to implement AsyncTask and loading message

VendasDataSource datasource;
private List<Venda> vendas;

protected void onCreate(Bundle savedInstanceState) {

Intent intent = getIntent();
String message = intent.getStringExtra(Vendas.EXTRA_MESSAGE);
String message1 = intent.getStringExtra(Vendas.EXTRA_MESSAGE1);

datasource = new VendasDataSource(this); // datasource class;

vendas = datasource.find("Select.... // my long query

//my custom list

ArrayAdapter<Venda> adapter = new VendaListAdapter(this, vendas);

my datasource class

public List<Venda> find(String select) {

String query = (select);

Cursor cursor = database.rawQuery(query, null);

Log.i(LOGTAG, "Returned " + cursor.getCount() + " rows");

List<Venda> vendas = cursorToList(cursor);
return vendas;

private List<Venda> cursorToList(Cursor cursor) { // MY LIST
List<Venda> vendas = new ArrayList<Venda>();
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
Venda venda = new Venda();


return vendas;

Where do I put the code of AsyncTask... thanks in advance

Answer Source

In your VendasActivity you should create & execute the AsyncTask (lets call it DataAsyncTask):

 DataAsyncTask dataAsyncTask = new DataAsyncTask();
 dataAsyncTask.execute(query); //query is your query string

The DataAsyncTask should be a inner class of VendasActivity and will have more or less this structure (assuming you want it to output a List<Venda> once it finished:

 private class DataAsyncTask extends AsyncTask<String, String, List<Venda>> {

      protected List<Venda> doInBackground(String... query) {
            String queryString = query[0];

            //TODO: add your long running database code here...

      protected void onPostExecute(List<Venda> vendaList) {

           Log.i(LOGTAG, "vendaList: " + vendaList);

For more details I recommend reading the documentation of AsyncTask here