【问题标题】:Getting data from async task onpostexecute using interface使用接口从异步任务onpostexecute获取数据
【发布时间】:2017-02-08 18:56:29
【问题描述】:

您好,我正在尝试从异步任务类获取数据数组列表到另一个主类:

我一直在关注下面的答案,但我有点迷茫:

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"

在我拥有的主要活动中:

`new GetVideoInfoFromDataBase(new GetVideoInfoFromDataBase.AlarmsDataBaseAsyncResponse()`

它想让我这样投射:

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

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

  1. 我不确定在覆盖 onprocessfinished 时如何返回结果。

提前感谢您的帮助

【问题讨论】:

标签: java android interface android-asynctask


【解决方案1】:

首先创建一个接口

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

【讨论】:

  • 那么最后一部分覆盖响应调用异步类?我不确定它是如何运行的?
  • AsyncTask 类在后台任务完成后,一旦返回到 PostExecute 方法,就会使用接口将结果数据发送到 mainactivity
  • 好的,但是你如何调用异步类呢?它在调用时要求上下文和异步接口作为参数
  • 新的 AsyncClass(Context, ?)
  • 对不起,我错误地添加了 AsyncInterface。你应该只传递 Context 作为 new AsyncClass(MainActivity.this)
【解决方案2】:

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

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

在 MainActivity 中,将您的逻辑推入:

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

【讨论】:

  • 我不明白你的解决方案。似乎第一部分已经在我的代码中了。
  • 你错过了默认构造函数子类中的 super 方法
猜你喜欢
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-04
  • 2020-10-10
  • 2020-02-09
相关资源
最近更新 更多