1

I have a Activity which contains a fragment with recyclerview. The recyclerview has few editable fields and need to put validations on clicking a button on activity.

Please let me know the solution or any techniques to overcome this problem statement

Akshay
  • 318
  • 1
  • 15
  • please add your recycler view code and the row you are using for recycler view as well. – Abdul Waheed Nov 29 '18 at 07:47
  • You can use interface to communicate between fragment and its activity. For validation of fields in recyclerView row, is it possible to include a validation button in recyclerView row instead the activity? It would make it easier for you to access fields in recyclerView adapter and validate. – ManishPrajapati Nov 29 '18 at 07:59
  • Possible duplicate of [Passing Data Between Fragments to Activity](https://stackoverflow.com/questions/14439941/passing-data-between-fragments-to-activity) – Ishita Sinha Nov 29 '18 at 08:07
  • @IshitaSinha Its not about passing data between fragment and activity. It's about a special scenario, where you have a recyclerview in fragment which has edittext and a button in activity. When the button is clicked we need to put a validation on edittext content. – Akshay Nov 29 '18 at 09:05
  • You can check this - https://stackoverflow.com/a/35395780/2128166 it has also same context to pass data between components so have look to it. – Wasim K. Memon Nov 29 '18 at 10:07
  • If the button is in the activity and the edittext is in the fragment, then it *is* about passing data between fragment and activity. – Ishita Sinha Nov 29 '18 at 10:15
  • @IshitaSinha edittext is in recyclerview – Akshay Nov 29 '18 at 11:56
  • and the recycler view is in the fragment – Ishita Sinha Nov 29 '18 at 16:41
  • @IshitaSinha recyclerview is in fragment only. The next button is in Activity. And the Recyclerview has a form containing Edittext and spinner. User enters name and select d gender from spinner. When i click next button i need to put a validatoin on form in recyclerview – Akshay Nov 30 '18 at 04:23
  • which is exactly why I said it _is_ about passing data between fragment and activity. – Ishita Sinha Nov 30 '18 at 06:09

3 Answers3

0

use this (click here ) functionality for data sharing between Fragment to fragment , Fragment to Activity and Activity to Activity

1) in your onCreate declare the BUSand register it

Bus bus = new Bus();
bus.register(this);

2) send data or any action using

bus.post(<your data or any Item>);

3) don't forgot to unregister in your onestroy

bus.unregister(this)
Ramkumar.M
  • 681
  • 6
  • 21
  • what about Activbity to Fragment or, Activity to Adapter class is it possible – Akshay Nov 29 '18 at 11:57
  • if you declare the adapter class in same activity means you can use interface... or the adapter class in Another fragment means you can go with this – Ramkumar.M Nov 29 '18 at 12:03
0

You can get that fragment instance either by 'ID' or by tag (you would need to give a tag while adding this fragment).

Now you can call any public method of that fragment using its instance.

For Example (Using tag), if you want to call a public method 'show()' of FragmentA. Then while adding it to the activity give it a tag like this:

Fragment fragmentA = new FragmentA();
getFragmentManager().beginTransaction()
.add(R.id.fragment_container,fragmentA,"YOUR_TARGET_FRAGMENT_TAG").commit(); 

And to get its instance (on a button click or as required):

FragmentA fragmentA = getFragmentManager().findFragmentByTag("YOUR_TARGET_FRAGMENT_TAG");
fragmentA.show();
Shivam
  • 123
  • 6
-1

Declare in your fragment the RecyclerView as public

Declare in your activity the fragment as variable

in your activity on click listener get the recyclerview with

fragment.recyclerview
Benjamin
  • 401
  • 1
  • 3
  • 16