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.

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

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.

  • ¿Cómo configurar el color de fondo personalizado mientras se toca el elemento?
  • RxJava: ¿Cómo puedo hacer una observación de búsqueda-una vez-y-reutilización?
  • ListView OnItemClickListener no responde?
  • Error extraño desde la actualización a Android 4.2.2 - Actividad ha filtrado IntentReceiver
  • ToUpperCase en Android es incorrecto para los argumentos de dos argumentos y el idioma griego y turco predeterminados
  • Tratando de reducir un mapa de bits en Android no funciona
  • Combinar dos archivos .m4a o convertir archivos .wav a m4a
  • ¿Cómo resolver AVD rotación automática tema de la pantalla en android 4.4.2?
  • Final de Android vs Vistas de texto privadas
  • Registrar el dispositivo en el archivo
  • Problema de codificación de Android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.