本文介绍了在活动中从 onLoadFinished 提交片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,它使用加载器回调从服务器加载数据列表.我必须将数据列出到一个扩展

I have an activity which loads a data list from the server using loader callbacks. I have to list out the data into a fragment which extends

SherlockListFragment

我尝试使用

Fragment newFragment   = CategoryFragment.newInstance(mStackLevel,categoryList);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.simple_fragment, newFragment).commit();

在 onLoadFinished 中,它给出了一个 IllegalStateException 说

in onLoadFinished and it gives an IllegalStateException saying

java.lang.IllegalStateException: Can not perform this action inside of onLoadFinished

我已经参考了 actionbar sherlock 中的示例,但这些示例在片段中包含加载器,而不是在活动中.

I have referred the example in actionbar sherlock, but those examples have loaders within the fragments and not the activity.

任何人都可以帮我解决这个问题吗?我可以在不从片段中调用加载器的情况下修复它!

Can anybody help me with this o that I can fix it without calling the loader from the fragment!

推荐答案

终于,我找到了解决这个问题的方法.创建一个设置空消息的句柄并调用该处理程序 onLoadFinished().代码与此类似.

Atlast, I have found a solution to this problem. Create a handle setting an empty message and call that handler onLoadFinished(). The code is similar to this.

@Override
public void onLoadFinished(Loader<List<Station>> arg0, List<Station> arg1) {
    // do other actions
    handler.sendEmptyMessage(2);
}

在处理程序中,

private Handler handler = new Handler()  { // handler for commiting fragment after data is loaded
    @Override
    public void handleMessage(Message msg) {
        if(msg.what == 2) {
            Log.d(TAG, "onload finished : handler called. setting the fragment.");
            // commit the fragment
        }
    }
};

片段的数量取决于需求.

The number of fragments depend on the requirement.

This method can be mainly used in case of stackFragments, where all fragments have different related functions.

这篇关于在活动中从 onLoadFinished 提交片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:01