简体   繁体   中英

How to get user inputs from multiple fragments added to single activity

As shown in image, I have added the same fragment 5 times in activity by performing on click operation on ADD FRAGMENT button:

Now I want to get all user entered data from those 5 edittext on click of GET DATA button. Is this possible?

(Both buttons is in Main Activity)

First we need to traverse to the dynamic edit text linear layout and then only we can count how many edit texts are avaliable then read the values of corresponding edit text :)

//step:1
LinearLayout layout = (LinearLayout)findViewById(R.id.LinearDynamicEditTextID);// change as your //dynamic EditTexts' parent Linear Layout id.

//step:2
for(int i=0;i<layout.getChildCount();i++)// Count how many children avaliable
 {
View v =  (View)layout.getChildAt(i);// get the child view
if (v instanceof EditText)// verifying whether the child is EditText for secure.
 {
 EditText editText =  (EditText)layout.getChildAt(i);//thats it.
 Log.w("Edit Text "+i+" Value" , editText.getText());
}

}

Now I want to get all user entered data from those 5 edittext on click of GET DATA button. Is this possible?

Answer: Yes, this is possible.

How? Solution: You can make public variables in the activity and you can access those variables from the fragment which you are adding. On click of GET DATA button, you can read those variables set by the different fragment and show them in the same Activity or in different Fragment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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