问题描述
我有一个活动从 FragmentActivity
延伸,我有片段的四个选项卡
,我有的AsyncTask
主 FragmentActivity
来从服务器获取负荷数据,因此,我想更新时完成AsyncTask的功能上的每个片段数据家长FragmentActivity,开始时用空白数据的活动,因为我使用 android.support.v4.view.ViewPager
所有片段的UI加载,所以,我想更新每个片段的领域(的TextView)与数据的AsyncTask得到,我怎么可以更新。鸭preciate你的帮助。使用下面的code为获得片段
I have an activity extends from FragmentActivity
, and I have four TABS of Fragments
, I have AsyncTask
in main FragmentActivity
to 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;
}
这是一个示例片段
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 {
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
}
推荐答案
创建片段方法,更新文本视图中的文本,从尾数活动访问片段的方法。
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();
这篇关于如何从FragmentActivity结束时,一个AsyncTask的更新片段的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!