tabLayout中如何从不同的fragments中传输数据?

How to transfer data from different fragments in tabLayout?

我在将 ListView 的选定项传递给另一个片段时遇到问题。

我有一个带有 2 个选项卡的 TabbedActivity。第一个叫做 OngletCours,第二个叫做 OngletNotes.

传递我单击的项目时出现错误。

我已经尝试了整个周末,但没有成功将我点击的项目转移到第二个tab/fragment。

这是我第一个 Fragment/Tab OngletCours 的代码(我只向您展示 setOnItemClickListener :

     l1.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            OngletNotes fragment = ((Onglets)getActivity()).getOngletNotes();

            if(fragment == null) {
                fragment = OngletNotes.newInstance();
            }
            //récupération de la position convertie en String de l'item que j'ai choisi
            String item = l1.getItemAtPosition(i).toString();

            Bundle args = new Bundle();
            args.putString("Item",item);

            fragment.setArguments(args);

            getFragmentManager().beginTransaction().add(R.id.container, fragment).addToBackStack(null).commit();
            ((Onglets)getActivity()).goToFragment(1);
        }

    });

我的第二个 tab/Fragment OngletNotes 看起来像这样:

public class OngletNotes extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.ongletnotes, container, false);
    //where i want to insert the selectedItem
    TextView Cours = (TextView)rootView.findViewById(R.id.TVCours);

    Bundle bundle=getArguments();
    String cours="";
    //ERROR !
    cours = bundle.getString("Item");
    //Retrieve the value
    Cours.setText(cours);

    return rootView;
}

public static OngletNotes newInstance() {
    OngletNotes fragment = new OngletNotes();
    // put values which you want to pass to fragment
    // Bundle args = new Bundle();
    // fragment.setArguments(args);
    return fragment;
}

我有以下错误:

03-06 12:48:13.959 1033-1033/com.example.dasilvadd.students E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.example.dasilvadd.students.OngletNotes.onCreateView(OngletNotes.java:23)

第 23 行如下:

Bundle bundle=getArguments();

请帮我解决这个问题,我真的需要推进我的项目。提前致谢!

使用共享首选项,在 OngletCours 中创建共享首选项,然后在 OngletNotes 中阅读。所有客户端共享此 class 的单个实例,因此在这种情况下,它使 sense.Go 变为此 link 以刷新它的代码语法。https://developer.android.com/training/basics/data-storage/shared-preferences.html

嘿,请记住这一点以备将来之用,无论何时存储数据,都要对其进行序列化。一个很棒的图书馆是 gson。 Gson 是一个 Java 库,可用于将 Java 对象转换为它们的 JSON 表示形式。它还可用于将 JSON 字符串转换为等效的 Java 对象。 Gson 可以处理任意 Java 对象,包括您没有源代码的预先存在的对象 of.Just 仍然需要考虑的事情。

试试这个

使用 Bundle 发送字符串:

YourFragment fragment = new YourFragment();
Bundle bundle = new Bundle();
bundle.putString("YourKey", "YourValue");
fragment.setArguments(bundle);

//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container,fragment).commit();

在新片段的onCreateView中:

//Retrieve the value
    String value = getArguments().getString("YourKey");

希望对你有用

为您的片段提供构造函数

 public OngletNotes () {
        setArguments(new Bundle());
    }

如果选项卡中只有两个片段,请不要使用 Bundle 传输数据。

我从头给你解释..

对于 ViewPager,您需要正确的片段列表。如下图

List<Fragment> fragmentsList = new ArrayList<Fragment>();
fragmentsList.add(Fragment1)-->OngletCours
fragmentsList.add(Fragment2)-->OngletNotes

您将在 ViewPagerAdapter 中传递上述列表。

在 Fragment2 中有一个函数如下所示

 public void getDataFromFragmentOne(String item){
 // Do the mannipulation
 }

当在FragmentOne中点击item的时候调用上面的函数如下

((OngletNotes)getParentFragment.fragmentsList.get(1)).getDataFromFragmentOne(item)..

上面的代码应该可以完美运行。因为你没有在 onCreateView 或 onCreate 中处理数据。每当单击该项目时,你都会将数据传递给 secondframent,由于 offsreenpage 限制,它已经存在于 viewpager 中。