本文介绍了AsyncTaskLoader不运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在使用兼容包我的项目的AsyncTaskLoader,所以我遵循的Andr​​oid文档的装载手册。

现在的问题是,该装载机什么都不做,似乎 loadInBackground()从不叫

什么是错误的,我code你知道吗?( ExpandableListFragment 扩展片段,但不覆盖任何重要的方法)

感谢您: - )

/ **编辑:

我意识到(晚了,我是一个白痴)的AsyncTaskLoader是一个抽象类,所以我需要继承它...米(__)米我离开万一有人来这里我后面的问题,谁知道......

 公共类AgendaListFragment扩展ExpandableListFragment工具
        LoaderManager.LoaderCallbacks< JSONArray> {

    私人的TreeMap<整数,ArrayList的< Evento>> mItems =新TreeMap的<整数,ArrayList的< Evento>>();
    私人AgendaListAdapter mAdapter;
    私人进度mProgressBar;

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        查看根= inflater.inflate(R.layout.fragment_agenda,集装箱);
        mProgressBar =(进度)root.findViewById(R.id.loading);
        返回根;

    }

    @覆盖
    公共无效onActivityCreated(包savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        mAdapter =新AgendaListAdapter(getActivity());
        setListAdapter(mAdapter);

        getLoaderManager()initLoader(0,空,这一点)。

    }

    @覆盖
    公共装载机< JSONArray> onCreateLoader(INT为arg0,捆绑ARG1){
        mProgressBar.setVisibility(View.VISIBLE);
        返回新AsyncTaskLoader< JSONArray>(getActivity()){
            @覆盖
            公共JSONArray loadInBackground(){

                返回getDataFromService(AgendaServices.LISTADO_MES);

            }

        };
    }

    @覆盖
    公共无效onLoadFinished(装载机< JSONArray>装载机,JSONArray数据){

        //一些东西把JSONArray到的TreeMap

        mProgressBar.setVisibility(View.GONE);
        mAdapter.setItems(mItems);

    }

    @覆盖
    公共无效onLoaderReset(装载机< JSONArray>为arg0){
        mAdapter.setItems(空);
        mProgressBar.setVisibility(View.VISIBLE);

    }

}
 

解决方案

我觉得对于兼容包中的最佳解决方案是重写 AsyncTaskLoader.onStartLoading 方式。

例如。

  @覆盖
保护无效onStartLoading(){
  如果(dataIsReady){
    deliverResult(数据);
  } 其他 {
    的forceload();
  }
}
 

I want to implement an AsyncTaskLoader in my project using the Compatibility Package, so I followed the Loader manual in Android Docs.

The problem is that the Loader does nothing, it seems loadInBackground() is never called

Any idea of what's wrong in my code?(ExpandableListFragment extends Fragment,but doesn't override any critical method )

Thank you :-)

/**EDIT:

I realized (late, I'm a moron) that AsyncTaskLoader is an abstract class so I need to subclass it... m(__)mI leave the question in case someone comes here behind me, who knows...

public class AgendaListFragment extends ExpandableListFragment implements
        LoaderManager.LoaderCallbacks<JSONArray> {

    private TreeMap<Integer, ArrayList<Evento>> mItems = new TreeMap<Integer, ArrayList<Evento>>();
    private AgendaListAdapter mAdapter;
    private ProgressBar mProgressBar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_agenda, container);
        mProgressBar = (ProgressBar) root.findViewById(R.id.loading);
        return root;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mAdapter = new AgendaListAdapter(getActivity());
        setListAdapter(mAdapter);

        getLoaderManager().initLoader(0, null, this);

    }

    @Override
    public Loader<JSONArray> onCreateLoader(int arg0, Bundle arg1) {
        mProgressBar.setVisibility(View.VISIBLE);
        return new AsyncTaskLoader<JSONArray>(getActivity()) {
            @Override
            public JSONArray loadInBackground() {

                return getDataFromService(AgendaServices.LISTADO_MES);

            }

        };
    }

    @Override
    public void onLoadFinished(Loader<JSONArray> loader, JSONArray data) {

        // Some stuff to turn JSONArray into TreeMap

        mProgressBar.setVisibility(View.GONE);
        mAdapter.setItems(mItems);

    }

    @Override
    public void onLoaderReset(Loader<JSONArray> arg0) {
        mAdapter.setItems(null);
        mProgressBar.setVisibility(View.VISIBLE);

    }

}
解决方案

I think the best solution for the Compatibility package is to override the AsyncTaskLoader.onStartLoading method.

e.g.

@Override
protected void onStartLoading() {
  if(dataIsReady) {
    deliverResult(data);
  } else {
    forceLoad();
  }
}

这篇关于AsyncTaskLoader不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 19:50