繁体   English   中英

如何在Android中的AsyncTask上返回匿名List或ArrayList

[英]How to return anonymous List or ArrayList on AsyncTask in Android

完成AsyncTask后,我有6种不同类型的List结果。 并且List结果应该返回给Activity 例如: List<A>List<B>List<C>List<D>List<E> ,最后是List<F>

这是我的AsyncTask

public class myAsync extends AsyncTask<String, String, List> {

    private List resultList;

    @Override
    protected List doInBackground(String... params) {
        //params[0] is url
        //params[1] is type
        callAPI api = new callAPI(params[0], params[1]);
        // According to type api decides type of List and generates
        return api.getJSON(); // returns a List which already parse JSON
    }

    @Override
    protected void onPostExecute(List result) {
        // Result is here now, may be 6 different List type.
        this.resultList = result 
    }

    // returns result
    public List getResultList() { return this.resultList; }
}

我会像这样调用AsyncTask:

myAsync task = new myAsync();
task.execute(params);
List myList = task.getResultList(); // type is uncertain
Log.d("Tag", Integer.toString(myList.size());

你知道,我必须指出<>标签之间的返回类型(Result)。 如果我为List选择特定类型,则它不适用于其他类型。

实际上,我已经尝试返回List<Object>并且只返回List类型。 但是没有用。

我不想使用6种不同的Async 是否有可能只用AsyncTask来解决这个问题? 我想我需要匿名列表或类似的东西不确定。 有谁能解释一下?

首先,我应该指出,您获取列表的顺序是正确的。 让我来证明:

// Initialize myAsync
myAsync task = new myAsync();

// This will do work for some period of time, this statement DOES NOT 'block'
// till myAsync completes, it will run it in the background
task.execute(params);

// This will return null because by the time you get here, task.execute most
// likely hasn't finished yet.
task.getResultList();

编辑:现在您已经包含了对列表结果执行的操作,以下是如何修改onPostExecute方法:

@Override
protected void onPostExecute(List result) {
  Log.d(TAG, "The returned list contains " +result.size()+ "elements");
  // Do anything else you need to with the returned list here
}

总而言之,如果您需要对返回的列表执行任何其他操作(除了打印它们的大小),例如比较,打印项目等,您需要在onPostExecute方法中执行此onPostExecute

根据列表的内容(A,B,C等),您可以为它们创建一个界面。

因此,如果A,B,C等是对象,则可以创建一个我们称之为ListContentInterface的接口。 然后,每个元素都必须实现ListContentInterface。 然后,您可以将类型指出为:

List<ListContentInterface>.

之后,您可以通过获取列表的第一个元素并检查它的类来测试列表内容的真正含义:

if(element.getClass() == ClassToTestFor.class) ...

如果对象有任何共同的方法,则应在界面中指定它们。 如果它们具有共同的ALL方法,则可以直接使用List列表,而无需测试对象的类(因为它们都可以执行接口定义的操作)。

我希望这对你有意义。 它可能不是最好的接口使用或最佳解决方案,但它可能会解决您的问题。

我正在使用execute()解决这类问题.get();

call from activity

    ArrayList<String> arraylistitem= new jsonparsing(getActivity()).execute().get();

    In async task 

    public class jsonparsing extends AsyncTask<Void, Void, ArrayList<String>>
    {
    public jsonparsing(Activity activity)
        {
            this.activity = activity;
            arraylistitem = new ArrayList<String>();        
        }

        protected ArrayList<String> doInBackground(Void... arg0) 
        {
               // Do anything else you need to with the returned list here
             return arraylistitem;
            }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM