如何将数据从一个滑动选项卡传递到另一个? [英] How to pass data from one swipe tab to another?

查看:84
本文介绍了如何将数据从一个滑动选项卡传递到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将两个字符串从第一个选项卡传递到第二个选项卡。虽然我写了这段代码:

I need to pass two string from the first tab to the second tab. Although I wrote this code:

 Map map = new Map();
            Bundle bundle = new Bundle();
            bundle.putString("Position", intent.getStringExtra("Position"));
            map.setArguments(bundle);
            Toast.makeText(context,"Visualizzare la posizione sulla mappa",Toast.LENGTH_SHORT).show();

在头等舱中,此代码为:

in the first class, and this code:

 Bundle args = getArguments();

    if(args != null)
    {
     Toast.makeText(getActivity().getBaseContext(),"Non è null",Toast.LENGTH_SHORT).show();
     String pos=args.getString("Position");
     String id=args.getString("ID");
    }

在第二堂课中,我的应用程序崩溃了。我不明白为什么。
我进行了很多搜索以查找原因,但我不知道该怎么做。
您能告诉我如何将这两个字符串从第一个片段传递到第二个片段吗?
非常感谢

in the second class, my app is crashed. I don't understand why. I searched a lot to found the reason but I don't understand how I can do. Could you tell me how could I pass this two strings from the first fragment to the second fragment? Thanks a lot

推荐答案

要在片段之间进行通信,必须遵循以下过程:
-您必须定义片段和父级活动之间的接口通信器,然后通过bundle中的设置参数传递数据。

For communicating between fragments you must follow this process : - you must define interface communicator between fragment and parent activity and then pass your data by set arguments in bundle .

要定义片段之间的接口通信器: http://developer.android.com/training/basics/fragments/communicating.html

To define interface communicator between fragments: http://developer.android.com/training/basics/fragments/communicating.html

设置参数:

Fragment fragment = new Fragment();
 final Bundle bundle = new Bundle();
 bundle.putString("data", data);
 Log.i("BUNDLE", bundle.toString());
 fragment.setArguments(bundle); 

以下是该过程:FragmentA --Data-> FragmentActivity --Data-> FragmentB

Here is the process : FragmentA --Data--> FragmentActivity --Data--> FragmentB

1-从片段定义接口通讯器回调

1 - Define interface communicator callback from fragment

interface communicate {
  public void sendData(String data);
} 

2-在父级活动中实施此通信器

2 - in parent activity implement this communiactor

import android.os.Bundle;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity implements communicate{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void sendData(String data) {
        // TODO Auto-generated method stub

    // From fragments we can call this method with the help of reference of communicate. 
    }

}

3-在第一个子段中,我们必须从创建的活动中获取communiactor的实例:

3 - In first fragment child ,we must get the instance of communiactor from on activity created :

public class FragmentA extends Fragment implements OnClickListener{
 Button button;
 communicate cm;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        return inflater.inflate(R.layout.layout_fragmenta, container, false);
    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        **cm = (communicate) getActivity();**
        button = (Button) getActivity().findViewById(R.id.button1);
        button.setOnClickListener(this);
    }



    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        cm.sendData();
    }

}

4-在您的父母活动中从方法sendData获取数据并将数据通过Bundle传递给fragmentB:

4 - in your parent activity you get your data from methode sendData and pass data to fragmentB via Bundle :

@Override
        public void sendData(String data) {
            // TODO Auto-generated method stub

      Fragment fragment = new Fragment();
     final Bundle bundle = new Bundle();
     bundle.putString("data", data);
     Log.i("BUNDLE", bundle.toString());
     fragment.setArguments(bundle); 
        }

5-最终;通过获取参数方法从FragmentB获取数据:

5 - finally ; get your data from FragmentB by get arguments methods :

公共类ExampleFragment扩展了Fragment
{

public class ExampleFragment extends Fragment {

public static FragmentB newInstance()
{
    FragmentB fragment = new FragmentB();

    // arguments
    Bundle arguments = new Bundle();
    arguments.putString(ARGUMENT_PRODUCT_ID, productId);
    fragment.setArguments(arguments);

    return fragment;
}

public FragmentB() {}

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    // handle fragment arguments
    Bundle arguments = getArguments();
    if(arguments != null)
    {
    }

}

}

这篇关于如何将数据从一个滑动选项卡传递到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆