我有一個片段分頁器的活動,我想知道如何從片段調用位於該活動的asynctask。
AddActivity.java在片段的父活動中調用AsyncTask
public class AddActivity extends FragmentActivity {
ViewPager vp;
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
vp = (ViewPager) findViewById(R.id.pager);
PagerAdapter pa = new FragPagerAdapter(getSupportFragmentManager());
vp.setAdapter(pa);
}
//This void to get All field from the differents fragments and put them into jsonObject
protected JSONObject createJsonObjectVR() {
...
}
// Call the Post Method
public class addVR extends AsyncTask<String, String, String> {
...
}
OtherFragment.java
public class OtherFragment extends Fragment {
Button btn;
public OtherFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_other, container, false);
btn = (Button) view.findViewById(R.id.send);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Here I want to call the createVR asynctask in the parent Activity
}
});
return view;
}
}
FragPagerAdapter
public class FragPagerAdapter extends FragmentPagerAdapter {
public FragPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// First Fragment The main
return new FirstFragment();
case 1:
// Milk (Second) fragment activity
return new SecondFragment();
case 2:
// Housing (Third) fragment activity
return new HousingFragment();
case 3:
//Feeding (fourth) fragment activity
return new FeedingFragment();
case 4:
// Other (Fith the last) fragment activity
return new OtherFragment();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 5;
}
}
從哪裏你叫otherfragment? – 2014-11-06 12:21:35
new addVR()。execute(params); – 2014-11-06 12:23:44
@Chelbta只是通過你在這個片段中的主要活動的參考,然後稱之爲 – 2014-11-06 12:27:52