此tutorial指的是片段之间的通信,但不会对标签进行通信。我想从我的" Daycare"之间发送数据。片段,这是我和你的标签;你"片段也是一个标签。我已经被困了一个星期了。我真的不知道如何将接口的概念与来自asynctasks的android选项卡式片段和数据相结合。
我在我的Daycare片段中创建了一个界面。我想发送String" daycarename"到了#34;你"在" passparam"的帮助下片段化方法。根据我的理解,它需要以某种方式通过实现我的TabClickedListener接口的MainActivity。如何将它从MainActivity传递回另一个片段?
public class MainActivity extends Activity implements ActionBar.TabListener, DaycareFragment.TabClickedListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager(), this);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new YouFragment();
case 1:
return new DaycareFragment();
case 2:
return new ThirdFragment();
}
return null;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section3).toUpperCase(l);
case 1:
return getString(R.string.title_section1).toUpperCase(l);
case 2:
return getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
}
public class MainFragment extends Fragment {
private static final String ARG_SECTION_TYPE = "section type";
public MainFragment(){}
public MainFragment(int sectionNumber) {
Bundle args = new Bundle();
args.putInt(ARG_SECTION_TYPE, sectionNumber);
setArguments(args);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//setup the view
switch(getArguments().getInt(ARG_SECTION_TYPE)) {
//hide or show fields based on page number.
}
return rootView;
}
}
@Override
public void passParam(String var) {
Toast.makeText(this, "Clicked " + var, Toast.LENGTH_LONG).show();
}
}
我在ListFragment中实现了一个接口:
public class DaycareFragment extends ListFragment {
TabClickedListener listener;
public interface TabClickedListener {
public void passParam(String var);
}
String email;
UserFunctions userFunctions;
Boolean owner;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_daycare, container, false);
movies = new ArrayList<HashMap<String, String>>();
userFunctions = new UserFunctions();
HashMap map = new HashMap();
map = userFunctions.getdauser(getActivity());
email = (String) map.get("email");
new GetDaDaycares().execute();
return rootView;
}
class GetDaDaycares extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... args) {
String city = "london";
try {
List<NameValuePair> params = new ArrayList<NameValuePair> ();
params.add(new BasicNameValuePair("city", city));
@SuppressWarnings("unused")
JSONObject json = parser.makeHttpRequest(getdaycare, params);
jArray = json.getJSONArray("lTable");
for (int i =0; i<jArray.length();i++){
JSONObject c = jArray.getJSONObject(i);
String daycarename = c.getString("daycarename");
HashMap<String, String> map = new HashMap<String, String>();
map.put("daycarename", daycarename);
movies.add(map);
}
} catch(JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String zoom){
pDialog.dismiss();
getActivity().runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(getActivity(), movies,
R.layout.list, new String[] {"daycarename"},
new int[]{R.id.textView1});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String daycarename =movies.get(position).get("daycarename");
}
});
}
});
}
}
}
答案 0 :(得分:1)
如果这是我的问题(它已经存在),我将有一个中心对象,负责在片段之间“共享”数据。
实现通常似乎遵循2个路径中的1个:一,创建一个单个,任何对象都可以获得一个或两个实例,该活动初始化一个对象的单个实例,并在初始化时将其传递给每个片段。
然后,片段(或AsyncTask)将通过观察者模式或显示来更新和从该中心对象中提取数据,无论您想要什么。P.S。 如果要在片段中使用AsyncTask,则需要实现一种策略,以确保UI在完成时不会死亡。否则你可以抛出异常。
p.p.s 默认情况下,onPostExecute在UI线程上运行。
答案 1 :(得分:1)
在Activity
:
public void passStrToYou(String daycarename)
{
FragmentManager fm = getFragmentManager();
Fragment youFrag = (YouFragment)fm.FragmentManager fm.findFragmentById(R.id.youFragment);
//call mathod 'setDayCareName' in 'you' fragment
youFrag.setDayCareName(daycarename);
}
希望这有帮助!