2015-02-28 5 views
3

Как я могу вернуть ArrayList объектов и строку из одного класса aSyncTask.Возврат ArrayList и String asyncTask

aSyncTask использует jsoup для извлечения 3 элементов таблицы, которые помещаются в новый класс Employee, а затем возвращаются в MainActivity в качестве arraylist, но я хочу, чтобы он также возвращал строку для этой конкретной таблицы.

MainActivity

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; 

Как бы я возвращать строку из здесь, а также в ArrayList сотрудника

+0

на посту выполнить возвратить arraylist к вашему методу или что-то еще. –

+0

Я обновил свой ответ после проверки. –

ответ

2

Другой подход может быть следующим ... В вашем GetEmployee класс оных следующие строки:

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

} 

Теперь в вашем 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(); 
1

Использование Карта как результат затем

Map<String,Object> data=new HashMap<String, Object>(); 
data.put("employees",ArrayList<Employee>); 
data.put("title", title); 

затем верните карту в doInBackground.

Надеюсь, это вам поможет.

2

Вы можете создать свой собственный класс для ответа

, например

class AsynckResult{ 
    public List<Employee> list; 
    public String someString; 

    //ctor and methods 

} 

просто исправить ваш общий в вашем AsyncTask

class YouAsyncTask extends AsyncTask<Void, Void, AsynckResult> 

ваша реализация будет выглядеть следующим образом

@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; 

, но я бы вернуть нулевое значение, если он работал улов

Смежные вопросы