2014-09-02 53 views
-1

我有一個活動從FragmentActivity延伸,我有Fragments四個選項卡,我在主FragmentActivityAsyncTask從服務器獲取負荷數據,所以,我想更新每個Fragment數據在完成父FragmentActivity上的AsyncTask函數時,因爲我使用的是android.support.v4.view.ViewPager所有Fragment的UI加載時用空白數據啓動Activity,所以,我想用從AsyncTask獲取的數據更新每個片段字段(TextView) ,我如何更新。感謝你的幫助。使用中獲取片段如何從FragmentActivity更新分段的TextView時光潔度的AsyncTask

@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擴展FragmentActivity實現ActionBar.TabListener {

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

}

+1

顯示一些代碼。 – Piyush 2014-09-02 09:13:51

+0

該代碼基本活動與碎片的TABS,使用SectionsPagerAdapter mSectionsPagerAdapter; – 2014-09-02 09:15:39

+0

你可以在onResume()中使用條件處理,如果onPost得到執行,然後將該標誌標記爲true,並在那裏執行更新代碼 – 2014-09-02 09:17:53

回答

0

在fra中創建方法更新文本視圖中的文本的gment,從片段活動訪問片段方法。

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

我可以知道這是什麼「Demo_Fragment」以及在哪裏可以找到它? – 2014-09-02 09:25:36

+0

「Demo_Fragment」是您在添加/替換片段時給出的標籤。 – Dory 2014-09-02 09:27:44

+0

我打電話這樣的片段,\t \t @Override \t \t公共片段的getItem(INT位置){ \t \t \t開關(位置){ \t \t \t情況下0: \t \t \t \t片段fragmenthome =新HomeFragment(); \t \t \t \t \t \t \t \t return fragmenthome; \t \t \t \t \t \t \t案例1: – 2014-09-02 09:29:14