简体   繁体   English

如何将数据从片段活动传递到片段

[英]How to pass data from fragment activity to fragments

I am doing service call in FragmentActivity where I need to pass two datas(name and address) to Fragment A and Fragment B respectively. 我在FragmentActivity中进行服务调用,我需要将两个数据(名称和地址)分别传递给Fragment A和FragmentB。

Here is my code which I tried. 这是我尝试的代码。

 public class C extends FragmentActivity {
// public PassDataToFragments fragmentCommunicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.detail_pager);
    serviceCall();
}

void serviceCall() {
    JSONObject totalData = new JSONObject(result);
    JSONObject jsonData = totalData.getJSONObject("result");
    trainerStrName = jsonData.getString("name");
    trainerStrAddress = jsonData.getString("address");
    // activityCommunicator.passDataToFragment(trainerStrName); i am
    // null pointer exception
    // pass name to fragment A and address to fragment B
}

@Override
public SherlockFragment getItem(int arg0) {
    switch (arg0) {

    // Open Previous Projects
    case 0:
        FragmentA fragmentA = new FragmentA ();
        return fragmentA;

        // Open Certification
    case 1:
        FragmentB fragmentB = new FragmentB ();
        return fragmentB;

    }
    return null;
}
}

What I tried is I used interface but i don't know how to use and get the values of name to FragmentA and address to Fragment B: 我试过的是我使用接口,但是我不知道如何使用并将名称值分配给FragmentA,并将地址值分配给Fragment B:

 public interface PassDataToFragments {
 public void passDataToFragment(String someValue);
  }
 class Fragment A extends Fragment implement PassDataToFragment{
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.trainers_certification,
            container, false);
 return view;
 }
 @Override
  public void passDataToFragment(String someValue) {
    //Log.e("string",someValue);

}
}

Pass the data from activity like this 这样传递活动中的数据

Bundle bundle = new Bundle();
bundle.putString("name", name);
bundle.putString("address", address);
FragmentA fragmentA = new FragmentA();
fragmentA.setArguments(bundle);

and in Fragment A onCreateView method retrieve it like this: 并在Fragment中的onCreateView方法像这样检索它:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {   
    return inflater.inflate(R.layout.fragment, container, false);
    String name = getArguments().getString("name");    
    String address = getArguments().getString("address"); 
}

EDIT 编辑

If fragment is already loaded then you can do it like this (using your current implementation) 如果片段已经被加载,那么您可以这样做(使用当前的实现)

Send data like this 发送这样的数据

PassDataToFragment fragA = (PassDataToFragment) getSupportFragmentManager().findFragmentById(R.id.container); // Change the id as per yours
fragA.passDataToFragment(name, address);

You will get the data in passDataToFragment() inside fragment A. 您将在片段A内的passDataToFragment()获得数据。

You can simply access the Activity's data by calling Activity's method from fragment : 您可以通过从fragment调用Activity的方法来简单地访问Activity的数据:

Get the value in Fragment : 获取Fragment中的值:

String name = ((C)getActivity()).getValue();
Toast.makeText(getActivity(), name, Toast.LENGTH_SHORT);

C Class : C级

public String getValue()
{
   return trainerStrDescription;
}

Hope it helps ツ 希望对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM