user1281921
user1281921

Reputation: 163

Return an ArrayList and String asyncTask

How would i return an ArrayList of objects and a String from one aSyncTask class.

The aSyncTask uses jsoup to retrieve 3 elements of a table which is put into a new Employee class and then returned back to MainActivity as an arraylist but i want it to also return a String for that specific table.

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;

How would i return a String from here as well as an ArrayList of Employee

Upvotes: 3

Views: 915

Answers (3)

SorryForMyEnglish
SorryForMyEnglish

Reputation: 1181

You can create your own class to response

for example

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

  //ctor and methods

}

just fix your generic in your AsyncTask

class YouAsyncTask extends AsyncTask<Void, Void, AsynckResult>

your implementation will now look like this

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

but I would return null if it worked catch

Upvotes: 2

dudego
dudego

Reputation: 896

Another approach can be as follows... In your GetEmployee class add following lines:

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

}

Now in your 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();

Upvotes: 2

Nagaraju V
Nagaraju V

Reputation: 2757

Use Map as result then

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

then return Map in doInBackground.

Hope this will helps you.

Upvotes: 1

Related Questions