0

我对 Fragment 有一个问题,它不会在 2.2 上调用 onPostExecute,而是在 2.3 上调用最新版本的 android。

我试图找到解决方案几天但没有找到。

我这个片段的代码

public class MovieFragment extends SherlockFragment implements IHomeScreen, OnItemClickListener, IActivity {

private FrameLayout      loading          = null;
private ListView         listView         = null;
private ListMovieAdapter listMovieAdapter = null;
private boolean          isFavorite       = false;
private boolean          isSearch         = false;
private String           strSearch        = null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    return inflater.inflate(R.layout.fragment_movie, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    init(view);
    initObjectListener();
    super.onViewCreated(view, savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    if (strSearch == null) {
        loadingDataFromLocal();
    }
    else {
        loadingDataFromServer(strSearch);
    }
    super.onActivityCreated(savedInstanceState);
}

@Override
public void init() {
}

@Override
public void initFormValidation() {
}

@Override
public void init(View v) {
    listView = (ListView) v.findViewById(R.id.listView);
    loading = (FrameLayout) v.findViewById(R.id.loading);
    loading.setVisibility(View.GONE);
}

@Override
public void initObjectListener() {
    listView.setOnItemClickListener(this);
}

@Override
public void loadingDataFromServer() {
}

public void loadingDataFromServer(String strSearch) {
    if (Utils.isOnline(getActivity())) {
        new LoadingDataFromServer().execute(strSearch);
    }
}

public void refreshList(List<EntryVO> entryVOs) {
    listMovieAdapter = new ListMovieAdapter(getSherlockActivity(), entryVOs, isFavorite);
    listView.setAdapter(listMovieAdapter);
}

@Override
public void loadingDataFromLocal() {
    Utils.log(new Exception(), "[isFavorite]" + isFavorite);
    if (isFavorite) {
        refreshList(DataSourceDAO.getFavorite(getSherlockActivity()));
        return;
    }
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    if (Utils.isOnline(getActivity())) {
        String youtubeID = listMovieAdapter.getItem(position).getYoutubeID();
        Utils.log(new Exception(), "[position]" + position + "[youtubeID]" + youtubeID);
        new YoutubePlayer(getSherlockActivity(), youtubeID);
    }
    else {
        Toast.makeText(getActivity(), R.string.no_internet_connection, Toast.LENGTH_LONG).show();
    }
}

class LoadingDataFromServer extends AsyncTask<String, Void, List<EntryVO>> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Utils.log(new Exception(), "[onPreExecute]");
        if (isSearch) {
            Utils.showProgressDialog(R.string.searching, getSherlockActivity());
        }
    }

    @Override
    protected void onPostExecute(List<EntryVO> result) {
        super.onPostExecute(result);
        Utils.log(new Exception(), "[onPostExecute]");
        Utils.dismissProgressDialog();
        refreshList(result);
    }

    @Override
    protected List<EntryVO> doInBackground(String... params) {
        Utils.log(new Exception(), "[doInBackground]");
        String search = params[0];
        List<EntryVO> entryVOs = new SearchYoutubeAPI().searchYoutube(search);
        Utils.log(new Exception(), "[entryVOs]" + entryVOs.size());
        return entryVOs;
    }
}

}

但如果我测试它复制

class LoadingDataFromServer extends AsyncTask<String, Void, List<EntryVO>> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Utils.log(new Exception(), "[onPreExecute]");
        if (isSearch) {
            Utils.showProgressDialog(R.string.searching, getSherlockActivity());
        }
    }

    @Override
    protected void onPostExecute(List<EntryVO> result) {
        super.onPostExecute(result);
        Utils.log(new Exception(), "[onPostExecute]");
        Utils.dismissProgressDialog();
        refreshList(result);
    }

    @Override
    protected List<EntryVO> doInBackground(String... params) {
        Utils.log(new Exception(), "[doInBackground]");
        String search = params[0];
        List<EntryVO> entryVOs = new SearchYoutubeAPI().searchYoutube(search);
        Utils.log(new Exception(), "[entryVOs]" + entryVOs.size());
        return entryVOs;
    }
}

要在 Activity 中运行,它也适用于 2.2。

你能告诉我我的代码有什么问题吗......

非常感谢,

4

0 回答 0