saeed norouzi saeed norouzi - 18 days ago 9
Android Question

public void implementation does not work in fragment

I am running an AsyncTask in main activity and result of that is a string vector. I want to use this vector to change a list view in the fragment. I use an interface (Asyncresponce) to get the vector by public void (processFinish) and then I implemented Asyncresponce in fragment and use it to clear and fill the Array adapter but public void does not work I use a logging statement to understand if this is running but I don't see it on device monitor.

I do not know what is the problem. Important parts of main activity and asynctask:

AsyncResponse myasync = new AsyncResponse() {
public void processFinish(String output[]) {

}
};

MainActivity.FetchWeatherTask weatherTask = new MainActivity.FetchWeatherTask(myasync);
//MainActivity.FetchWeatherTask weatherTask = new MainActivity.FetchWeatherTask();
weatherTask.execute("232931","82da4ddd2e8380be6dea06b177926b2d");


on post execute where I pass string vector on processFinish:

Log.v(LOG_TAG, "on post execute:+: "+ resultStrs[1]);
delegate.processFinish(resultStrs);


and fragment:

public class MainActivityFragment extends Fragment
implements MainActivity.AsyncResponse {
public String[] weatherdata = new String[5];
public ArrayAdapter<String> mForecastAdapter;
public final String LOG_TAG_fragmet =MainActivityFragment.class.getSimpleName();

@Override
public void processFinish(String output[]) {
if (output!= null){
Log.v(LOG_TAG_fragmet, "this is from fragment="+ output[1]);
mForecastAdapter.clear();
}
}

Answer

You should pass your MainActivityFragment instance as the argument to the call new MainActivity.FetchWeatherTask(myasync);

The instance of AsyncResponse you are passing has an empty implementation for processFinish(Object[]), so you're not actually calling back to MainActivityFragment

Something like this maybe:

public class MainActivityFragment extends Fragment
implements MainActivity.AsyncResponse {
public String[] weatherdata = new String[5];
public ArrayAdapter<String> mForecastAdapter;
public final String LOG_TAG_fragmet    =MainActivityFragment.class.getSimpleName();

@Override
public void  processFinish(String output[]) {

    if (output!= null){
        Log.v(LOG_TAG_fragmet, "this is from fragment="+ output[1]);
        mForecastAdapter.clear();
    }
}

public void someMethod() {
   MainActivity.FetchWeatherTask weatherTask =  new  MainActivity.FetchWeatherTask(this);
   weatherTask.execute("232931","82da4ddd2e8380be6dea06b177926b2d");
}
Comments