Get EditText value on different fragment

2019-02-27 09:36发布

问题:

I am trying to get some text from editTexts on different fragments. So what I do first is define my mPager and mPagerAdapter:

a_Atenuacion Activity

public class a_Atenuacion extends FragmentActivity{

    private static final int NUM_PAGES = 3;
    /**
     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
     */
    private ViewPager mPager;

    /**
     * The pager adapter, which provides the pages to the view pager widget.
     */
    private PagerAdapter mPagerAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.a_dat_viewpager);

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);


    }
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            switch(position){
                case 0:
                    return new a_Dat_Inicio1();
                case 1:
                    return new a_Dat_Inicio2();
                case 2:
                    return new a_Dat_Inicio3();
            }
            return null;
        }
        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
}

Then I get my 3 fragments classes (Both 1st and 2nd layouts have an editText, but the 3rd one has an editText and a button). The button function is that when I am in the last fragment (fragment3) it take info form (different editTexts) and send to another activity.

a_Dat_Inicio1 Fragment

public class a_Dat_Inicio1 extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Test", "hello");
    }

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

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

        return view;  
    }
}

a_Dat_Inicio3 Fragment

public class a_Dat_Inicio3 extends Fragment {

    EditText edit3;
    EditText edit2;
    EditText edit1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Test", "hello");
    }

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

    }

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


        edit1 = (EditText)getActivity().findViewById(R.id.editText1);
        final String edit11 = edit1.getText().toString();
        edit2 = (EditText)getActivity().findViewById(R.id.editText2);
        final String edit22 = edit2.getText().toString();
        edit3 = (EditText)view.findViewById(R.id.editText3);
        final String edit33 = edit3.getText().toString();

        Button but=(Button) view.findViewById(R.id.button);

        but.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {

                //Creamos el bundle
                Bundle bundle = new Bundle();
                //Le ponemos la variable parametro con el contenido (key, valor)
                bundle.putString("edit3", edit33);
                bundle.putString("edit2", edit22);
                bundle.putString("edit1", edit11);
                Intent net= new Intent(v.getContext(),Prueba1.class);
                net.putExtras(bundle);
                startActivity(net);

            }
        });

        return view;
    }
}

Finally I get bundle on another activity (Prueba1.class) and it is curious that I only get result for editText1 (on 1st fragment) and rest are null.

Can anybody give me a help?

Thanks in advance.

回答1:

finally I get over an interface, that is the only way I think to get soemthing on already defined fragments.

My code get like this:

1st define an interface

public interface OnEditTextChanged {

public void onEditPressed1(String edit1);

public void onEditPressed2(String edit1);

public void onEditPressed3(String edit1);

Then fragment activity:

public class a_Dat_Inicio1 extends Fragment {

EditText edit;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e("Test", "hello");
}

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

}

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

    return view;
}

OnEditTextChanged editListener;
@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    try{
        editListener=(OnEditTextChanged) getActivity();
    }catch(ClassCastException e){
        throw new ClassCastException(activity.toString()+"must implemnt onEditPressed");
    }
}

private void init(View view) {
    edit=(EditText) view.findViewById(R.id.editText1);

    //cada vez que se modifique texto llamar
    edit.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            final String edit11 = edit.getText().toString();
            editListener.onEditPressed1(edit11);

        }
    });

And finally on our main activity, call the method:

public class a_Atenuacion extends FragmentActivity implements OnEditTextChanged {

String dat1;
String dat2;
String dat3;

private static final int NUM_PAGES = 3;
private PagerAdapter mPagerAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.a_1dat_viewpager);

    // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);


    //HERE DO WHATEVER YOU WANT WITH THE DATA CAUGTH ON THE EDIT 1 METHOD


}


@Override
public void onEditPressed1(String edit1) {

    if(mPager.getCurrentItem()==0){
        dat1=edit1;
        Toast bread = Toast.makeText(getApplicationContext(), "edit1", Toast.LENGTH_LONG);
        bread.show();
    }
}

HOPE this help someone!!!

In any way thanks!!!



回答2:

Try this:

In your second fragment, pass the intent extras and your value. For example:

In you second fragment, do this :

Intent someintent = new Intent();
        //Some first result
        someintent.putExtra("your_value_one", your_value_one);
        //Some Second result
        someintent.putExtra("your_value_two", your_value_two);
        //Some third result
        someintent.putExtra("your_value_three", your_value_three);
        getActivity().setResult(getActivity().RESULT_OK,someintent);
        getActivity().finish();

In your other fragment, where you want this result, do this on your other fragment like this:

1) Make some method to get the info.

private void getInfo() {

                    //Note: The values are coming from a diff activity or fragment and                 so the strings should match.
                    Intent data = new Intent();
            String first_value = data.getStringExtra("your_value_one");
                    String second_value = data.getStringExtra("your_value_two");
                    String third_value = data.getStringExtra("your_value_three");
            Log.i("First Value: ", first_value);
                    Log.i("First Value: ", second_value);
                    Log.i("First Value: ", third_value);

        }

After making this method, just call it on your onActivityCreated().

Now you can use those string however and wherever you want to. If you want to use those values anywhere else, make sure to define you strings at the very start so that you can use the values anywhere.

Hope this answer helps .. :)