将数据从A和B片段传递到C片段

时间:2014-06-16 12:04:21

标签: android android-fragments

我创建了三个片段 A B C 。在片段 A 中,我有名字姓氏。片段 B 我有年龄城市区域地址。在 C 我有专业经验。在frag C 中,我还有一个按钮,它将所有信息发送到服务器。现在,我如何从片段 C 中的片段 A B 获取信息。我使用Bundle发送信息,但这是一项繁琐的工作。有没有简单的方法?

E.g:

Bundle args = new Bundle ();
args.putString ("first_name", strFirstName);
args.putString ("last_name", strLastName);

3 个答案:

答案 0 :(得分:2)

不是将数据从片段A和B传递到C,而是通过回调将它们传递给Activity,然后传递“发送到服务器”动作回调,以便Activity处理它:< / p>

class Fragment A {
// fragment definition

    public interface OnUserInformationTypedListener {
        public void onUserInformationTyped(String name, String lastName);
    }
}

class FragmentB {
// fragment definition

    public interface OnUserExtraInformationTypedListener {
        public void onUserExtraInformationTyped(int age, String address);
    } 
}

class FragmentC {
// fragment definition

    public interface OnUserCareerInformationTypedListener {
        public void onUserCareerTyped(String profession, String experience);
        public void onSendToServer();
    }
}

然后让Activity实现所有接口:

class MyActivity extends Activity implements OnUserInformationTypedListener, 
    OnUserExtraInformationTypedListener, OnUserCareerInformationTypedListener {

    @Override
    public void onUserInformationTypedListener(String name, String lastName) {
         // Probably pass local variable to private attributes
    }

    // Override the rest of the interface's methods

    @Override
    public void onSendToServer() {
        // Send information to the server logic
    }
}

最后,让每个FragmentAFragmentBFragmentC成为各自回调的实例,即:

// Inside FragmentA
private OnUserInformationTypedListener listener = null;

然后通过调用Fragment#onAttach(Activity)或通过公共方法将侦听器引用传递给它:

// Inside FragmentA
@Override
public void onAttach(Activity activity) {
    listener = (OnUserInformationTypedListener) activity;
}

// if you prefer public setter then create the setter and call it from the Activity:
// Inside Activity#onCreate or wherever you instantiate the fragment
FragmentA fa = new FragmentA();
fa.setOnUserInformationTypedListener(this);

有关如何通过主持人Fragments Activity进行沟通的更多信息(这是正确的方法),请阅读此http://developer.android.com/training/basics/fragments/communicating.html

答案 1 :(得分:1)

Fragment documentation说:

  

通常,您会希望一个片段与另一个片段进行通信,例如根据用户事件更改内容。所有Fragment-to-Fragment通信都是通过相关的Activity完成的。两个碎片永远不应该直接沟通。

因此,将消息从一个片段传递到另一个片段并不是一个好主意。查看basics fragment training docs

  

有任何简单的方法吗?

您可以将数据保存在公共类中,并从其他片段中访问它们。

答案 2 :(得分:0)

使用此我想在按钮点击上传递数据,如此

    btn_camera.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Bundle bundle = new Bundle();
            bundle.putString("edttext", "From Activity");
            // set Fragmentclass Arguments
            Fragmentclass fragobj = new Fragmentclass();
            fragobj.setArguments(bundle);
        }
    });

获取片段的数据oncreate方法

@Override

public View onCreateView(LayoutInflater inflater,ViewGroup容器,         Bundle savedInstanceState){     String strtext = getArguments()。getString(&#34; edttext&#34;);
    return inflater.inflate(R.layout.fragment,container,false); }

相关问题