3

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

2
  • on post execute return the arraylist to your method or anything. Commented Feb 28, 2015 at 10:39
  • I updated my answer once check. Commented Feb 28, 2015 at 10:42

3 Answers 3

2

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();
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.