I'm trying to fill a Spinner by clicking a button. Have the function run through an AsyncTask so it doesn't slow down the app.
MainActivity.java
package com.example.sigeapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ArrayList<String> arrList_obras;
private ArrayAdapter<String> arrAdapter_obras;
private Spinner spinner_obras;
private Button button;
CONEXIONES conexiones = new CONEXIONES();
private MitareaAsincrona mitarea;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner_obras = findViewById(R.id.spinner_prueba);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mitarea = new MitareaAsincrona();
mitarea.execute();
}
});
}
private void tareaLarga()
{
arrList_obras = conexiones.SELECT("SELECT * FROM OBRAS", getApplicationContext(), 3);
arrAdapter_obras = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_drawable, arrList_obras);
spinner_obras.setAdapter(arrAdapter_obras);
}
private class MitareaAsincrona extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.e("AsyncTask", "onPreExecute");
}
@Override
protected Boolean doInBackground(Void... params) {
try {
Log.v("AsyncTask", "doInBackground");
tareaLarga();
return true;
} catch (Exception ex) {
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
Log.v("AsyncTask", "onPostExecute");
Toast.makeText(MainActivity.this, "Tarea finalizada!", Toast.LENGTH_SHORT).show();
}
}
}
}
The function tareaLarga()
is responsible for loading the data into Spinner
it and it works fine. If I put a Toast
in the onClick
del button
is before or after mitarea.execute()
it Toast
is shown but the AsyncTask does not work, I realize this by the 3 logs and the Toast that I put, none of them do anything. What am I doing wrong?
Update
If the Asynctask runs but does not load the Sipnner. the Toast and the logs if they are executed
As your code is written there should be no problem executing the
AsyncTask
, you should do it this wayThe problem is probably due to the fact that the query you are making does not return a list of elements in
arrList_obras
, you must ensure that the query obtains data so that they fill yourArrayAdapter
:Update:
You comment that "if I do it without using the Asynchronous task if they load perfectly", then the problem is with loading the data.
Within the method
tareaLarga()
that is performed withindoInbackground()
theAsynctask
performs only the data collection:but within
onPostExecute()
theAsyncTask
which is where you show the changes to the UI from the data obtained indoInbackground()
, configure yourAdapter
with the data