Android Fragment未替换为PostExecute AsyncTask

时间:2013-01-10 20:21:32

标签: android tabs android-asynctask

我正在尝试从asynctask类替换片段onPostExecute方法,但它不会替换任何东西。这是我的标签监听器类:

public class TabListener implements ActionBar.TabListener
{
    protected ParentActivity parent;
    public FragmentTransaction fragmentTransaction;
    public ItemData item = new ItemData();

    public TabListener(ParentActivity parent)
    {
        this.parent = parent;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        this.fragmentTransaction = ft;

        if(this.parent.isNetworkAvailable())
        {
            //ft.replace(android.R.id.content, new LoadingFragment());

            if(tab.getPosition() == 0)
            {
                new HttpData().execute(ParentActivity.centroUri);

            }
            else
            {
                new HttpData().execute(ParentActivity.nvomxliUri);
            }
        }
        else
        {
            ft.replace(android.R.id.content, new NetworkFragment());
        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }


    private class HttpData extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {
            Parser p = new Parser();
            p.run(urls[0], item);
            return "";
        }

        @Override
        protected void onPostExecute(String result) {
            fragmentTransaction.replace(android.R.id.content, new LaneFragment(item));
        }

    }
}

我确认已执行onPostExecute方法,并按预期检索所需数据。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您是否尝试在PostExecute中创建新的片段事务?您使用的是onTabSelected发送给您的相同事务,但我认为该事务在该方法结束时提交。

    FragmentTransaction fragmentTransaction = mFragmentMgr.beginTransaction();

    // Add fragment to the container ContentView 
    fragmentTransaction.replace(R.id.fragment, tag);

    fragmentTransaction.addToBackStack(null);

    // Commit transaction
    fragmentTransaction.commit();