Fragment to Fragment EditText

时间:2017-08-04 13:00:21

标签: java android android-fragments

I would like to pass my EditText input from my first fragment to the textview on the second fragment on button click. But textview displays "null" value when button click is triggered.

First Fragment:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.first_frag, container, false);

    TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
    tv.setText(getArguments().getString("msg"));

    Button btn = (Button) v.findViewById(R.id.tryBtn);
    btn.setOnClickListener(new View.OnClickListener() {
                               @Override
                               public void onClick(View v) {
                                   EditText woo = (EditText)v.findViewById(R.id.editText);
                                   MainActivity.myBundle.putString("Try", String.valueOf(woo));
                                   switch (v.getId()) {
                                       case R.id.tryBtn:
                                      SecondFragment home = new SecondFragment();
                                           FragmentTransaction ft = getFragmentManager().beginTransaction();
                                           ft.replace(R.id.first_frag, home);
                                           ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
                                           ft.addToBackStack(null);
                                           ft.commit();
                                           break;
                                   }
                               }
                           }
    );

    return v;
}

Second Fragment:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.second_frag, container, false);

    TextView tv = (TextView) v.findViewById(R.id.tvFragSecond);
    TextView tvTry = (TextView) v.findViewById(R.id.tvTry);
    String myValue = (String) MainActivity.myBundle.get("Try");

    return v;
}

MainActivity:

public static Bundle myBundle = new Bundle();

4 个答案:

答案 0 :(得分:1)

what about calling newInstance with parameters?

SecondFragment.newInstance(/*your text*/)  //when you open your fragment

//in your Second Fragment
 public static SecondFragment newInstance(String data) {
    SecondFragment sFragment = new SecondFragment ();
    Bundle bundle = new Bundle();
    bundle.putString("data", data);
    sFragment.setArguments(bundle);
    return sFragment;
    }

答案 1 :(得分:0)

Use Bundle for that, show below code;

  Fragment fragment = new SecondFragment();
  FragmentManager fm =getSupportFragmentManager();
  FragmentTransaction ft = fm.beginTransaction();
  ft.replace(R.id.frame_layout, fragment);
  ft.commit();

  Bundle bundle = new Bundle();
  bundle.putString("id", woo.getText().toString());
  fragment.setArguments(bundle);

and get this value in SecondFragment like below code;

Bundle bundle = this.getArguments();
      if (bundle != null) {
         String id  = bundle.getString("id");
      }
tv.setText(id);

答案 2 :(得分:0)

About your statement :

But textview displays "null" value when button click is triggered.

This is NOT the way to get a text from editText

EditText woo = (EditText)v.findViewById(R.id.editText);
String.valueOf(woo)

try this one:

woo.getText().toString()

About passing information between fragments I use EventBus

It uses the Observer pattern:

You subscribes to an event:

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(MessageEvent event) {/* Do something */};

You post events:

EventBus.getDefault().post(new MessageEvent());

That will work if you have both fragments at the same time. Considering your logic you do not have both fragments. So I would do the next:

Your fragment should call your activity like:

mainActivity.onSentInputField(woo.getText().toString());

And your activity will be responsible for changing the fragment with the correct information. This way you code is more organized and the fragments are being changed in one single place.

答案 3 :(得分:0)

试试这段代码,

First Fragment

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.first_frag, container, false);

//        TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
//        tv.setText(getArguments().getString("msg"));


        final EditText woo = (EditText)v.findViewById(R.id.editText);

        Button btn = (Button) v.findViewById(R.id.tryBtn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SecondFragment home = new SecondFragment();

                Bundle bundle = new Bundle();
                bundle.putString("Try", woo.getText().toString());
                home.setArguments(bundle);

                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.first_frag, home);
                ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
                ft.addToBackStack(null);
                ft.commit();

            }
        });

        return v;
    }

第二片段

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.second_frag, container, false);

    TextView tv = (TextView) v.findViewById(R.id.tvFragSecond);
    TextView tvTry = (TextView) v.findViewById(R.id.tvTry);

    Bundle arguments = getArguments();
    if (arguments != null) {
        String myValue  = arguments.getString("Try");
        tvTry.setText(myValue);
    }


    return v;
}

并在MainActivity中删除此行,最好不要使用静态包。

public static Bundle myBundle = new Bundle();