Android 我无法将字符串传递给片段
从片段调用片段。我需要向片段传递一个字符串。怎么做Android 我无法将字符串传递给片段,android,Android,从片段调用片段。我需要向片段传递一个字符串。怎么做 if (position==1){ FragmentTransaction ft; VideoList lf = new VideoList(); ft = getFragmentManager().beginTransaction(); ft.replace(R.id.fragmentsPanel, lf); ft.addT
if (position==1){
FragmentTransaction ft;
VideoList lf = new VideoList();
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragmentsPanel, lf);
ft.addToBackStack(null);
ft.commit();
}
我想传递字符串str=“absd”;第二个片段采用字符串str1=第一个片段的行使用参数
public static VideoList videoListWithString(String string) {
VideoList videoList = new VideoList();
Bundle arguments = new Bundle();
arguments.putString("testString","test");
videoList.setArguments(arguments);
return videoList;
}
在你的片段中,你创造了
Bundle arguments = getArguments();
String testString = arguments.getString("testString");
使用参数
public static VideoList videoListWithString(String string) {
VideoList videoList = new VideoList();
Bundle arguments = new Bundle();
arguments.putString("testString","test");
videoList.setArguments(arguments);
return videoList;
}
在你的片段中,你创造了
Bundle arguments = getArguments();
String testString = arguments.getString("testString");
可以在片段中使用setArguments()和getArguments()方法
像
在片段中,你可以得到这样的字符串
public class VideoList extends ListFragment {
public View onCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your arguments
Bundle bundle=getArguments();
//here is your list array
String str=bundle.getString("str");
}
}
可以在片段中使用setArguments()和getArguments()方法
像
在片段中,你可以得到这样的字符串
public class VideoList extends ListFragment {
public View onCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your arguments
Bundle bundle=getArguments();
//here is your list array
String str=bundle.getString("str");
}
}
如果您的需求是将值从一个片段传递到另一个片段,那么请尝试使用bundle 例如:
TalkDetail fragment = new TalkDetail();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("largeimg", largeimg);
bundle.putString("excert", excert);
bundle.putString("description",description);
bundle.putString("cat", cat);
bundle.putString("header_title", "Talk");
//bundle.putInt("postid", postid);
fragment.setArguments(bundle);
((BaseContainerFragment)getParentFragment()).replaceFragment(fragment, true);
这是您的BaseContainerFragment
类,它将帮助您获得更好的回溯和其他好东西
public class BaseContainerFragment extends Fragment {
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container_framelayout, fragment);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
public boolean popFragment() {
Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
boolean isPop = false;
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
isPop = true;
getChildFragmentManager().popBackStack();
}
return isPop;
}
}
如果您的需求是将值从一个片段传递到另一个片段,那么请尝试使用bundle 例如:
TalkDetail fragment = new TalkDetail();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("largeimg", largeimg);
bundle.putString("excert", excert);
bundle.putString("description",description);
bundle.putString("cat", cat);
bundle.putString("header_title", "Talk");
//bundle.putInt("postid", postid);
fragment.setArguments(bundle);
((BaseContainerFragment)getParentFragment()).replaceFragment(fragment, true);
这是您的BaseContainerFragment
类,它将帮助您获得更好的回溯和其他好东西
public class BaseContainerFragment extends Fragment {
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container_framelayout, fragment);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
public boolean popFragment() {
Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
boolean isPop = false;
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
isPop = true;
getChildFragmentManager().popBackStack();
}
return isPop;
}
}
使用接口作为对宿主活动的回调,然后在活动之间进行通信。文档hi中的示例签出my answerft.replace(R.id.fragmentsPanel,lf,links[position]);link=getTag();使用接口作为对宿主活动的回调,然后在活动之间进行通信。文档hi中的示例签出my answerft.replace(R.id.fragmentsPanel,lf,links[position]);link=getTag();