1

Hi I am trying to get an arraylist of data from a an async task class to another main class:

I was following the answer below but I am a little lost:

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

So I have my class that extends async task and calls to the database to get my object array:

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

    }
}

There are no errors but I think I have done something incorrectly in it causing my error.

So in my main activity to call the results I have:

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


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

    }
}).execute();

I have two problems here

  1. I am getting the error:
    "incompatible types: AsyncTask cannot be converted to GetVideoInfoFromDataBase"

In the mainactivity where i have:

`new GetVideoInfoFromDataBase(new GetVideoInfoFromDataBase.AlarmsDataBaseAsyncResponse()`

it wants me to cast it like this:

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

That doesn't seem right but I thought i would check.

  1. I am not sure how to return the result when overriding the onprocessfinished.

Thanks in advance for your help

Community
  • 1
  • 1
Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89

2 Answers2

2

First create an Interface

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

Assign it in the asynctask class as below :-

Context context;
Private AsyncInterface asyncInterface;

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

Then inside onPostExecute method of asynctask class :-

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

Then implement this interface in your activity :-

class MainActivity extends AppCompatActivity implements AsyncInterface {

and then import the method of asyncInterface

@Override
public void response(String response) {
    //Here you get your response
    Log.e(TAG, response);
}
Mahesh Babariya
  • 4,560
  • 6
  • 39
  • 54
1

Modify Constructor of class. Need default constructor. By the way, create method to set Interface.

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

In MainActivity, push your logic in:

  object.setInterface(new AlarmsDataBaseAsyncResponse(){
        @Override
        public void processFinish(PaginatedScanList<AlarmDynamoMappingAdapter> output) {
              //your logic
        }
  });
Nguyễn Hoàng
  • 179
  • 1
  • 11