cara get edittext value dari fragment lain

newbie gan mohon pencerahannya :)... saya punya 4 fragment didalam 1 mainactivity (viewpager) fragment1 = edittext1, fragment2 = edittext2, fragment3 = edittext3 & fragment4 = buttonsave

nah di fragment4 ini hanya ada button save untuk mengambil data dari fragment 1,2 dan 3 tapi ketika saya klik buttonsave, data dari fragment1 selalu null

MainActivity:


public class MainActivity extends AppCompatActivity {

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

        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new FragmentOne(), "FRAG1");
        adapter.addFragment(new FragmentTwo(), "FRAG2");
        adapter.addFragment(new FragmentThree(), "FRAG3");
        adapter.addFragment(new FragmentFour(), "FRAG4");
        viewPager.setAdapter(adapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

Fragment 1,2 dan 3

public class FragmentOne extends Fragment {
    public FragmentOne() {
        // Required empty public constructor
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

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

Fragment4

public class FragmentFour extends Fragment {

    private EditText txt1, txt2, txt3;
    private Button button;

    public FragmentFour() {
        // Required empty public constructor
    }

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

        txt1 = (EditText)getActivity().findViewById(R.id.txt1);
        txt2 = (EditText)getActivity().findViewById(R.id.txt2);
        txt3 = (EditText)getActivity().findViewById(R.id.txt3);

        button = (Button)view.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getData();
            }
        });
        return view;
    }

    private void getData() {
        String fragment1 = txt1.getText().toString();
        String fragment2 = txt2.getText().toString();
        String fragment3 = txt3.getText().toString();
    }
}

Fragment4 null

nah seperti itu gan saya gak bisa dapet data dari semua fragment, pas di debug null langsung crash... thanks gan ditunggu pencerahannya :)

avatar Gumilang
@Gumilang

1 Kontribusi 0 Poin

Dipost 7 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban