0

I have a fragment. Inside that fragment I am programmatically creating edittext widgets as follows:

driverItem = new EditText(getContext());
driverItemArray.add(driverItem);
driverItem.setId((int) dataSnapshot.getChildrenCount());
driverItem.setHint(people.getItem());
driverItem.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
lin.addView(driverItem);

So how can I get the edittext values from my fragment when pressing a button in my MainActivity?

UPDATE WITH NEW CODE

Here is my Mainactivity

public class MainActivity extends AppCompatActivity implements DriverFragment.OnFragmentInteractionListener{

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private TabLayout tabLayout;

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


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setOffscreenPageLimit(3);

     setupViewPager(mViewPager);//mViewPager.setAdapter(mSectionsPagerAdapter);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    setupTabIcons();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
            //        .setAction("Action", null).show();


    //BUTTON FIRES AND GET THE VALues FROM FRAGMENT
            onReturnValue(s);

        }
    });
}


private void setupTabIcons(){
    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new DriverFragment(), "");
    adapter.addFragment(new ErrorFragment(), "");
    adapter.addFragment(new InfoFragment(), "");
    viewPager.setAdapter(adapter);
}

@Override
public void onReturnValue(String driverItem) {
    System.out.println(driverItem);
}

Here is my fragment

public class DriverFragment extends Fragment{
ListView listView;

List<String> driverItemArray = new ArrayList<String>();

private OnFragmentInteractionListener mListener;

public DriverFragment(){

}

public interface OnFragmentInteractionListener {
    void onReturnValue(String driverItem);

}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(getActivity());
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){

    View view = inflater.inflate(R.layout.fragment_driver,container,false);
    listView = (ListView) view.findViewById(R.id.listViewDriver);

return view;

}

@Override
public void onStart(){
    super.onStart();
   FirebaseListAdapter<String> adapter = new FirebaseListAdapter<String>(
            getActivity(),
            String.class,
            R.layout.textview_layout,
            mRootRef) {
        @Override
        protected void populateView(View view, String s, int i) {
            TextInputLayout textInputLayout = (TextInputLayout)view.findViewById(R.id.input_layout);
            EditText editText = (EditText)view.findViewById(R.id.input_edittext);
            editText.setId(i);
            textInputLayout.setHint(s);
            driverItemArray.add(s);
        }
    };
    listView.setAdapter(adapter);
    Toast.makeText(getActivity().getApplicationContext(), listView.getAdapter().getCount()+"", Toast.LENGTH_SHORT).show();
}

}

3 Answers3

0

Try this it will save all EditText text in String array

String[] strings = new String[](driverItemArray.length);
for(int i=0; i < driverItemArray.length; i++){
    string[i] = driverItemArray[i].getText().toString();
}
Akshay Panchal
  • 695
  • 5
  • 15
0

The way to do this is with the callback pattern. So when you make the fragment it accepts an interface. When the button is called it calls a method on that interface. The activity can then be the one implementing the interface to receive the event. Android Studio is even able to create the boilerplate for you.

Inside your fragment

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

On the button click listener

onReturnValue(driverItem.getText().toString())

The interface itself

public interface OnFragmentInteractionListener { void onReturnValue(String driverItem); }

The activity

public class MyActivity implements OnFragmentInteractionListener  {
    ....

    public void onReturnValue(String value) {
       //youre inside the activity now so use the value
    }
}
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • I have multiple edittext. Is it possible to get all values at once? – Beard o Mania Aug 09 '16 at 12:35
  • Maybe I am implementing it wrong but the driverItem is created in the Fragment so I dont see how I will be able to use the **onReturnValue()** inside my **MainActiviy** – Beard o Mania Aug 09 '16 at 13:06
  • This is a really basic pattern you need to learn to use fragments. Try reading this then: https://developer.android.com/training/basics/fragments/communicating.html – Nick Cardoso Aug 09 '16 at 14:21
  • Ok. Now I am confused. Am a now using a viewpager to show my fragments. So I also need to set a fragment layout as in the link you referred to. Seems a bit much for just getting values from edittext widget. But I will give it a shoot – Beard o Mania Aug 09 '16 at 19:29
  • That article says nothing of the sort... Looking at it, I can't even guess what part you're talking about – Nick Cardoso Aug 10 '16 at 06:57
  • I am using android studios template for tabs and that generated code use a viewpager. – Beard o Mania Aug 10 '16 at 10:20
  • The pattern should be fine with any type of setup, what I don't understand is your comment about needing to set a fragment layout. You shouldn't need to do anything more than what I've done in my sample - Basically the activity implements a method and the fragment calls that method – Nick Cardoso Aug 10 '16 at 10:26
  • I have implemented your code and I don't get it to work. – Beard o Mania Aug 10 '16 at 20:51
  • Edit your question to include your new code (the stuff of mine that you've implemented) underneath – Nick Cardoso Aug 10 '16 at 21:17
-1

In Your Fragment declare driverItem as global variable and this method:

public String getEditTextValue() {
 return driverItem.getText().toString().trim();
}

From Activity you have an instance of the fragment. then call above method:

String value = fragmentInstance.getEditTextValue();
Naveen Kumar M
  • 7,497
  • 7
  • 60
  • 74