Return 一个 ArrayList 和 String asyncTask

Return an ArrayList and String asyncTask

我如何 return 对象的 ArrayList 和来自一个 aSyncTask 的字符串 class。

aSyncTask 使用 jsoup 检索 table 的 3 个元素,将其放入新的 Employee class 中,然后 returned 返回 MainActivity 作为数组列表,但我想要它也 return 一个特定的字符串 table.

主要活动

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

获取员工

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;

我如何 return 来自这里的字符串以及 Employee

的 ArrayList

使用 Map 作为结果然后

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

然后 return 在 doInBackground 中映射。

希望对您有所帮助。

您可以创建自己的class来回复

例如

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;

但如果有效,我会 return null

另一种方法可以如下... 在您的 GetEmployee class 中添加以下行:

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