【问题标题】:Return an ArrayList and String asyncTask返回一个 ArrayList 和字符串 asyncTask
【发布时间】:2015-02-28 10:11:42
【问题描述】:

我如何从一个 aSyncTask 类返回一个对象的 ArrayList 和一个字符串。

aSyncTask 使用 jsoup 检索表的 3 个元素,该表被放入新的 Employee 类中,然后作为数组列表返回到 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;

我如何从这里返回一个字符串以及一个员工的 ArrayList

【问题讨论】:

  • 在执行后将数组列表返回到您的方法或任何东西。
  • 我在检查后更新了答案。

标签: java android android-asynctask


【解决方案1】:

另一种方法可以如下... 在您的 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();

【讨论】:

    【解决方案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

    【讨论】:

      【解决方案3】:

      然后使用地图作为结果

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

      然后在 doInBackground 中返回 Map。

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-17
        • 2015-04-30
        • 2012-08-21
        • 1970-01-01
        • 2021-02-03
        • 1970-01-01
        • 2015-08-24
        • 2021-03-04
        相关资源
        最近更新 更多