2016-11-10 48 views
0

我正在運行一個AsyncTask在主要活動和結果那是一個字符串向量。我想用這個向量來改變片段中的列表視圖。我使用接口(Asyncresponce)通過public void(processFinish)獲取矢量,然後在fragment中實現Asyncresponce並使用它清除並填充Array適配器,但public void不起作用我使用日誌語句來理解這是運行,但我沒有看到它在設備監視器上。公共無效實施不起作用的片段

我不知道是什麼問題。 主要活動的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"); 

上後執行,我傳遞processFinish串矢量:

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

和片段:

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

回答

0

你應該通過您的MainActivityFragment實例作爲調用的參數new MainActivity.FetchWeatherTask(myasync);

的你逝去的AsyncResponse實例有processFinish(Object[])空的實現,這樣你就不會實際調用回MainActivityFragment

事情是這樣的,也許:

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"); 
} 
+0

是這個工作,但是我把它在Oncreateview上。非常感謝 – saeed