user3238711 user3238711 - 2 months ago 4
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(R.id.pesquisa);
Button button1 = (Button)vendas.findViewById(R.id.ven_revisoes);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {

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


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

VendasDataSource datasource;
private List<Venda> vendas;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_vendas);

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

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


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

//my custom list

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


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();

venda.setid(cursor.getLong(cursor.getColumnIndex.......
vendas.add(venda);
}
}

return vendas;
}


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

Answer

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...
      }

      @Override
      protected void onPostExecute(List<Venda> vendaList) {

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

For more details I recommend reading the documentation of AsyncTask here