Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

android - How to update Fragment TextView from FragmentActivity when finish a AsyncTask

I have an activity extends from FragmentActivity, and I have four TABS of Fragments, I have AsyncTask in main FragmentActivityto get load data from a server, so, I want to update each Fragment data when complete the AsyncTask function on parent FragmentActivity, since I'm using android.support.v4.view.ViewPager all the Fragment's UI loading when starts the Activity with blank data, so, I want to update the each fragment fields (TextView) with the data getting from AsyncTask, How I can update. Appreciate your help. The below code using for get Fragment

    @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;
    }

And here is a sample Fragment

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;
    }
}

here is the main Activity public class PatientActivity extends FragmentActivity implements ActionBar.TabListener {

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

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create method in fragment which updates the text in text view,access fragment method from the fragment activity.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...