我无法将字符串传递给片段

时间:2014-03-24 09:11:27

标签: android

从片段调用片段。我需要将一个字符串传递给片段。怎么做?

if (position==1){
            FragmentTransaction ft;
            VideoList lf = new VideoList();
            ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.fragmentsPanel, lf);
            ft.addToBackStack(null);
            ft.commit();
        }

我想传递String str =“absd”;第二个片段取String str1 =第一个片段的行

4 个答案:

答案 0 :(得分:2)

使用参数!

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

答案 1 :(得分:1)

您可以在Fragments中使用setArguments()和getArguments()方法

if (position==1){
            FragmentTransaction ft;
            VideoList lf = new VideoList();
            Bundle bundle = new Bundle();
            bundle.putString("str", "absd");
            ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.fragmentsPanel, lf);
            ft.addToBackStack(null);
            ft.commit();
        }

并在片段中得到这样的字符串

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

答案 2 :(得分:1)

Bundle params = new Bundle();
params.putString("str1", "absd");
getFragmentManager().beginTransaction()
                .replace(R.id.fragment_place, YourFragment.instantiate(getActivity(), YourFragment.class.getName(),  params), "YourFragmentTag").commit();

答案 3 :(得分:1)

如果您的要求是将值从一个片段传递到另一个片段,请尝试使用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;
            }

        }