使用接口从onynctexecute获取异步任务中的数据

时间:2016-09-30 08:51:22

标签: java android interface android-asynctask

您好我正在尝试从异步任务类获取数据的arraylist到另一个主类:

我正在按照下面的答案,但我有点失落:

How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

所以我的类扩展了异步任务并调用数据库来获取我的对象数组:

public class GetVideoInfoFromDataBase extends AsyncTask {

    // Paginated list of results for song database scan
    static PaginatedScanList<AlarmDynamoMappingAdapter> results;

    // The DynamoDB object mapper for accessing DynamoDB.
    private final DynamoDBMapper mapper;

    public interface AlarmsDataBaseAsyncResponse {
        void processFinish(PaginatedScanList<AlarmDynamoMappingAdapter> output);
    }

    public AlarmsDataBaseAsyncResponse delegate = null;

    public GetVideoInfoFromDataBase(AlarmsDataBaseAsyncResponse delegate){
        mapper = AWSMobileClient.defaultMobileClient().getDynamoDBMapper();
        this.delegate = delegate;
    }

    @Override
    protected Object doInBackground(Object[] params) {

        DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
        results = mapper.scan(AlarmDynamoMappingAdapter.class, scanExpression);
        return results;
    }

    @Override
    public void onPostExecute(Object obj) {

        delegate.processFinish(results);

    }
}

没有错误,但我认为我在其中做错了导致我的错误。

所以在我的主要活动中调用我的结果:

GetVideoInfoFromDataBase asyncTask =new GetVideoInfoFromDataBase(new GetVideoInfoFromDataBase.AlarmsDataBaseAsyncResponse(){


    @Override
    public void processFinish(PaginatedScanList<AlarmDynamoMappingAdapter> output) {

    }
}).execute();

我这里有两个问题

  1. 我收到错误:
    "incompatible types: AsyncTask cannot be converted to GetVideoInfoFromDataBase"
  2. 在我所拥有的主要活动中:

    `new GetVideoInfoFromDataBase(new GetVideoInfoFromDataBase.AlarmsDataBaseAsyncResponse()`
    

    它希望我像这样投射:

    (GetVideoInfoFromDataBase) new GetVideoInfoFromDataBase(new GetVideoInfoFromDataBase.AlarmsDataBaseAsyncResponse()
    

    这似乎不对,但我想我会检查。

    1. 我不知道在重写onprocessfinished时如何返回结果。
    2. 提前感谢您的帮助

2 个答案:

答案 0 :(得分:2)

首先创建一个接口

public interface AsyncInterface {
    void response(String response);
}

在asynctask类中分配它,如下所示: -

Context context;
Private AsyncInterface asyncInterface;

AsyncClassConstructor(Context context){
    this.context = context;
    this.asyncInterface = (AsyncInterface) context;
}

然后在asynctask类的onPostExecute方法中: -

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    asyncInterface.response(s);
}

然后在您的活动中实现此界面: -

class MainActivity extends AppCompatActivity implements AsyncInterface {

然后导入asyncInterface

的方法
@Override
public void response(String response) {
    //Here you get your response
    Log.e(TAG, response);
}

答案 1 :(得分:1)

修改类的构造函数。 需要默认构造函数。顺便说一句,创建方法来设置接口。

public void setInterface(AlarmsDataBaseAsyncResponse delegate){
         this.delegate = delegate;}

在MainActivity中,将您的逻辑推送到:

  object.setInterface(new AlarmsDataBaseAsyncResponse(){
        @Override
        public void processFinish(PaginatedScanList<AlarmDynamoMappingAdapter> output) {
              //your logic
        }
  });