返回一个ArrayList和String的异步任务

3

如何从一个aSyncTask类中返回一个对象的ArrayList和一个String。

aSyncTask使用jsoup来检索表格的3个元素,这些元素被放入一个新的Employee类中,然后作为ArrayList返回到MainActivity,但我也希望它能够返回该特定表格的一个String。

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

获取员工信息

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;

我该如何在这里返回一个String和一个Employee的ArrayList?


在post execute中,将ArrayList返回给您的方法或其他任何内容。 - Surender Kumar
我已经更新了我的回答,请检查。 - Nagaraju V
3个回答

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;

但是如果成功了,我会返回 null,否则就会捕获异常。


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作为结果。
 Map<String,Object> data=new HashMap<String, Object>(); 
 data.put("employees",ArrayList<Employee>);
 data.put("title", title);

然后在doInBackground中返回Map。
希望这能帮助到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接