簡體   English   中英

使用接口從異步任務onpostexecute獲取數據

[英]Getting data from async task onpostexecute using interface

嗨,我正在嘗試從異步任務類到另一個主類獲取數據的數組列表:

我一直在關注以下答案,但我有點迷茫:

由於AsyncTask是一個單獨的類,如何使OnPostExecute()的結果進入主要活動?

因此,我有了擴展異步任務並調用數據庫以獲取對象數組的類:

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"

在我有的mainactivity中:

`new GetVideoInfoFromDataBase(new GetVideoInfoFromDataBase.AlarmsDataBaseAsyncResponse()`

它希望我像這樣投射它:

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

這似乎不正確,但我想我會檢查的。

  1. 我不確定在重寫onprocessfinished時如何返回結果。

在此先感謝您的幫助

首先創建一個界面

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

修改類的構造方法。 需要默認構造函數。 順便說一句,創建方法來設置接口。

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

在MainActivity中,將邏輯推入:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM