[英]How to return anonymous List or ArrayList on AsyncTask in Android
I've 6 different type of List
results after completing AsyncTask
. 完成
AsyncTask
后,我有6种不同类型的List
结果。 And the List
results are should be returned to an Activity
. 并且
List
结果应该返回给Activity
。 For example: List<A>
, List<B>
, List<C>
, List<D>
, List<E>
and finally List<F>
. 例如:
List<A>
, List<B>
, List<C>
, List<D>
, List<E>
,最后是List<F>
。
This is my AsyncTask
: 这是我的
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; }
}
I'll call AsyncTask like this: 我会像这样调用AsyncTask:
myAsync task = new myAsync();
task.execute(params);
List myList = task.getResultList(); // type is uncertain
Log.d("Tag", Integer.toString(myList.size());
You know, I must point out the return type (Result) between <> tags. 你知道,我必须指出<>标签之间的返回类型(Result)。 If I choose specific type for
List
, it does not work for other types. 如果我为
List
选择特定类型,则它不适用于其他类型。
Indeed, I already tried to return List<Object>
and only List
types. 实际上,我已经尝试返回
List<Object>
并且只返回List
类型。 But doesn't worked. 但是没有用。
I don't want to use 6 different Async
. 我不想使用6种不同的
Async
。 Is it possible to solve this with an only AsyncTask
? 是否有可能只用
AsyncTask
来解决这个问题? I guess I need anonymous List or something similiar not sure. 我想我需要匿名列表或类似的东西不确定。 Can anyone explain this?
有谁能解释一下?
First, I should point out that the sequence in which you're obtaining the list is not correct. 首先,我应该指出,您获取列表的顺序是不正确的。 Let me demonstrate:
让我来证明:
// 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();
Edit: Now that you've included what you want to do with the list result, here's how you would modify your onPostExecute
method: 编辑:现在您已经包含了要对列表结果执行的操作,以下是如何修改
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
}
So to summarize, if you needed to do anything else with the returned lists (besides printing their size), for example compare, print items, etc, you need to do that in the onPostExecute
method. 总而言之,如果您需要对返回的列表执行任何其他操作(除了打印它们的大小),例如比较,打印项目等,您需要在
onPostExecute
方法中执行此onPostExecute
。
Depending on what the contents of the list is (What are A, B, C etc.), you might be able to make an interface for them. 根据列表的内容(A,B,C等),您可以为它们创建一个界面。
So if A, B, C etc are objects, you can make an Interface that we call ListContentInterface. 因此,如果A,B,C等是对象,则可以创建一个我们称之为ListContentInterface的接口。 Each of your elements must then implement the ListContentInterface.
然后,每个元素都必须实现ListContentInterface。 You can then point out the type as:
然后,您可以将类型指出为:
List<ListContentInterface>.
After that you can then test what the contents of the list really is, by taking the first element of the list, and checking it's class: 之后,您可以通过获取列表的第一个元素并检查它的类来测试列表内容的真正含义:
if(element.getClass() == ClassToTestFor.class) ...
If the objects have any methods in common, you should specify them in the interface. 如果对象有任何共同的方法,则应在界面中指定它们。 If they have ALL methods in common, you can use the List list directly, without testing for the class of the objects (As they can all do what the interface defines).
如果它们具有共同的ALL方法,则可以直接使用List列表,而无需测试对象的类(因为它们都可以执行接口定义的操作)。
I hope this makes some sense to you. 我希望这对你有意义。 It might not be the best use of interfaces or the best solution, but it might solve your problem.
它可能不是最好的接口使用或最佳解决方案,但它可能会解决您的问题。
I'm solving this kind of problem using execute().get(); 我正在使用execute()解决这类问题.get();
Example 例
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.