gpt4 book ai didi

java - 使用选项卡和 onTabSelected() 时如何在 fragment 之间传递数据?

转载 作者:行者123 更新时间:2023-12-02 04:55:43 29 4
gpt4 key购买 nike

这个tutorial指的是 fragment 之间的通信,但不用于选项卡。我想在我的“Daycare” fragment (它是一个选项卡)和我的“You” fragment (它也是一个选项卡)之间发送数据。我已经被这个问题困住了一个星期了。我真的不知道如何将接口(interface)的概念与 android 选项卡式 fragment 和来自 asynctasks 的数据结合起来。

我在我的 Daycare fragment 中创建了一个界面。我想借助“passparam”方法将字符串“daycarename”发送到“you” fragment 。据我了解,它需要以某种方式通过实现我的 TabClickedListener 接口(interface)的 MainActivity。如何将它从 MainActivity 传递回另一个 fragment ?

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 中实现一个接口(interface):

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");

}

});


}
});
}

}

}

最佳答案

如果这是我的问题(确实如此),我会有一个中央对象负责在 fragment 之间“共享”数据。

实现通常似乎遵循两条路径之一:一,创建一个任何对象都可以获取其实例的单例,或者二, Activity 初始化对象的单个实例,并在初始化时将其传递给每个 fragment 。

然后, fragment (或 AsyncTask)将通过观察者模式或显示方式更新并从该中心对象中提取数据,无论您想要什么。

附:如果您要在 fragment 中包含 AsyncTask,您将需要实现一种策略来确保您的 UI 在完成时不会死掉。否则你可以抛出异常。

p.p.sonPostExecute 默认在 UI 线程上运行。

关于java - 使用选项卡和 onTabSelected() 时如何在 fragment 之间传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28790011/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com