Devolver un ArrayList y String asyncTask
Cómo devolvería una ArrayList de objetos y una String de una clase aSyncTask.
El aSyncTask utiliza jsoup para recuperar 3 elementos de una tabla que se pone en una nueva clase Employee y luego devuelve a MainActivity como arraylist pero quiero que también devuelva una String para esa tabla específica.
- Cómo recibir la última emisión de secuencia al llamar a rx.Observable.sample ()?
- Obtener resultado de una actividad después de terminar (); En una prueba de unidad de Android
- ¿Cómo incluir archivos '.jar' en React-native para Android?
- El uso de un HashMap para almacenar grandes cantidades de datos está ralentizando mi aplicación de Android, ¿hay otras opciones?
- ¿Cómo puedo eliminar el "+" y el código del país de un número de teléfono?
Actividad principal
public void onButtonClick() new GetEmployee() { @Override protected void onPostExecute(ArrayList<Employee> list) { Intent intent = new Intent(getApplicationContext(), EmployeeActivity.class); intent.putParcelableArrayListExtra("empClass", list); //i want to also pass the title of the Employee table aswell as the list, //after running the aSyncTask startActivity(intent); } }.execute();
GetEmployee
public Employee empObj; @Override protected ArrayList<Employee> doInBackground(String... params) { ArrayList<Employee> emp = new ArrayList<Employee>(); String title; try { //get employee details from table using JSoup //get employee table title using JSoup empObj = new Emp(...); emp.add(empObj); return emp; } catch (IOException e) { e.printStackTrace(); } return emp;
¿Cómo iba a devolver una cadena de aquí, así como un ArrayList de empleado
- Cómo inicializar el contenido de la pestaña cuando se inicializa la aplicación
- Java 7/8 en Android?
- Inyección de múltiples componentes independientes
- Android Visualización de datos de json a cardview en RecyclerView
- ¿Cómo implementar Moquette broker en Android?
- Private String o public static String?
- ¿Existe ya una clase StopWatch para android y por qué no funciona mi implementación?
- ¿Cómo puedo enviar desde el lado del servidor (Google App Engine, Cloud Endpoints) la información a mi cliente?
Otro enfoque puede ser el siguiente … En su clase GetEmployee, añada las siguientes líneas:
abstract class GetEmployee{ // your declaration String title; //my return string @Override protected ArrayList<Employee> doInBackground(String... params){ ArrayList<Employee> emp = new ArrayList<Employee>(); String title; try { //get employee details from table using JSoup //get employee table title using JSoup empObj = new Emp(...); bus.add(empObj); title="myTitle" //fetch some title value here...2 return emp; } catch (IOException e) { e.printStackTrace(); } return emp; } @Override protected void onPostExecute(ArrayList<Employee> list) { myPostExecute(list,title); } public abstract void myPostExecute(ArrayList<Employee> emp,String title); }
Ahora en su MainActivity:
public void onButtonClick() new GetEmployee() { @Override protected void myPostExecute(ArrayList<Employee> list,String title) { Intent intent = new Intent(getApplicationContext(), EmployeeActivity.class); intent.putParcelableArrayListExtra("busClass", list); intent.putString("title",title);//this is your title startActivity(intent); } }.execute();
Puede crear su propia clase a la respuesta
por ejemplo
class AsynckResult{ public List<Employee> list; public String someString; //ctor and methods }
sólo tienes que arreglar tu genérico en tu AsyncTask
class YouAsyncTask extends AsyncTask<Void, Void, AsynckResult>
su implementación ahora se verá así
@Override protected AsynckResult doInBackground(String... params) { AsynckResult result = new AsynckResult(); ArrayList<Employee> emp = new ArrayList<Employee>(); String title; try { //get employee details from table using JSoup //get employee table title using JSoup empObj = new Emp(...); emp.add(empObj); result.list = emp; result.someString = title; return result; } catch (IOException e) { e.printStackTrace(); } return result;
pero volvería nulo si funcionaba pescar
Utilice Mapa como resultado entonces
Map<String,Object> data=new HashMap<String, Object>(); data.put("employees",ArrayList<Employee>); data.put("title", title);
luego devuelve Mapa en doInBackground.
Espero que esto te ayude.
- Separar horas de minutos en un valor de tiempo dado
- Error: archivos duplicados durante el empaquetado de APK Android Studio Error