如何在完成AsyncTask时从FragmentActivity更新Fragment TextView

时间:2014-09-02 09:12:14

标签: android android-fragments android-asynctask

我的活动范围从FragmentActivity延伸,我有四个Fragments的TABS,我在主AsyncTask中有FragmentActivity来从服务器获取加载数据,所以,我想在父FragmentActivity上完成AsyncTask函数时更新每个Fragment数据,因为我在使用空白数据启动Activity时使用android.support.v4.view.ViewPager所有Fragment的UI加载,所以,我想要使用从AsyncTask获取的数据更新每个片段字段(TextView),如何更新。感谢您的帮助。以下代码用于获取片段

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            Fragment fragmenthome =  new HomeFragment();                
            return fragmenthome;                
        case 1:
            Fragment fragmentdiagnosis =  new DiagnosisFragment();              
            return fragmentdiagnosis;
        case 2:
            Fragment fragmentcareplan =  new CareplanFragment();                
            return fragmentcareplan;
        case 3:             
            Fragment fragmentnote =  new NoteFragment();                
            return fragmentnote;
        case 4:
            Fragment fragmenttask =  new TaskFragment();                
            return fragmenttask;                    
        }
        return null;
    }

这是一个样本片段

public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy,container, false);
        TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
        dummyTextView.setText("Comming Soon...");
        return rootView;
    }
}

这里是主要的活动 公共类PatientActivity extends FragmentActivity实现了ActionBar.TabListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_patient);
    // AsyncTask function calling

}

1 个答案:

答案 0 :(得分:0)

在片段中创建方法,在文本视图中更新文本,从片段活动中访问片段方法。

DemoFragment demo_frag = (DemoFragment)getSupportFragmentManager().findFragmentByTag("Demo_Fragment");
 demo_frag.updateTextView();