public void实现在片段中不起作用

时间:2016-11-10 21:27:08

标签: android

我在主要活动中运行 AsyncTask ,结果是字符串向量。我想使用此向量来更改片段中的列表视图。我使用接口(Asyncresponce)通过public void(processFinish)获取向量然后我在片段中实现Asyncresponce并使用它来清除和填充数组适配器但是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");

on post execute我在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();
    }
  }

1 个答案:

答案 0 :(得分: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");
}