Passing ArrayList from Fragment to Activity and Using BottomNavigationView - java

I am having the following problem, my application has two fragments, one with a Spinner where it contains the contents of the ArrayList and another fragment containing a ListView, I want to pass the contents of the Spinner ArrayList to the Activity to send it to the other fragment, I am doing so because I'm using a BottomNavigationView. Any idea how to do it?

your Fragments have context. This context could be casted to your Activity.
in Fragment
((YourActivity) getContext()).send(contents);

Related

Get UI Element ID In a Fragment From Custom adapter

I'm developing an android app and I have a fragment that contains TextView & ListView in it. The list view has custom list items that contains two buttons. I want to make 'onClickListener' for one of those two buttons in my custom adapter class to change the text of the TextView, but I can't access it by findViewById() every time I try I got null exception.
My guess is that you're trying to access the TextView from inside of the Adapter.
If so, then you won't be able to get the TextView and it's normal to get a nullpointer exception.
The findViewById inside your adapter only finds the views that you have inflated in the getView() method of your adapter.
What you can do here is probably use an interface that's implemented by your Fragment to pass the information from your adapter back to the fragment.
This answer might be a good starting point : How to create interface between Fragment and adapter?

RecyclerView inside PagerAdapter

I have a main fragment that contains a viewpager. This viewpager gets the same secondary fragment (different from the main one) but with different parameters every time. Inside the said secondary fragment that is inside the viewpager, i have a recyclerview. Inside these recyclerviews are some fields that the user fills up. When the main fragment (the one that holds the viewpager) is closed (via a button click) i need to get the data from each recyclerview. How do i to that?
The best way to solve this problem is that implement interface which will give call back to activity holding these fragment and then from Activity pass it to fragment(main) and then use viewpager to get Fragment(secondry) and pass data.
Steps one would be implent interface which will give callback to activity and the get fragment from viewpager(Link for same)

How to send data between fragments with SlidingTabLayout

Basically I am trying to create an app that passes data filled in an EditText on on one fragment, into a TextView on another fragment on a button click(The buttons is in the first fragment with the EditText). I use the SlidingTabLayout. I have 2 java classes that both extend Fragment and both inflate separate xml layouts(in the onCreateView). I have a java MainActivty with a public class"SectionsPagerAdapter that extends FragmentPagerAdapter, which depending on the swipe of the user displays 1 of the 2 Fragment classes. I am really confused on how I can send data between the 2 fragment sot that from the EditText in 1 fragment can get sent to the TextView in the other fragment on a button click. Could the suggested solutions be explained as simple as possible because I am relatively new to Android Studio. Many thanks in advance.
As per my understanding, basically you want to pass data between two fragments.
You can use activity for that from where fragments are initialized.
you can do this.
in MainActivity.java:
have a function setData(Object obj) and getData() which returns that object.
From fragment:
You can call those function of activity to save your data and get your data.
Here's way:
MainActivity activity = (MainActivity) getActivity();
Object obj = activity.getData();
activity.setData(obj);
I hope it helps.

Passing arraylist from multiple fragments to activity

I have a follow up question from my previous questions.
My app starts and fragment loads, it passes in the arraylist correctly using interface and listener method .. Now when I click on navigation button to replace current fragment with another fragment... That fragment is exactly the same as first fragment but with different array list... How would I update the arraylist in activity?
I get a classcast exception.
Here are how both of my fragments are implemented
Pass Arraylist from fragment to its own Activity
In terms of the ClassCastException, did you make sure to have your MainActivity(know its MainActivity from MainActivity.java:107) implement the OnFragmentInteractionListener? If you don't that could be why you're getting that ClassCastException, because you're trying to cast your an activity instance(getActivity) to a subclass of which it does not share any inheritance relationship with (OnFragmentInteractionListener)

Accessing Fragment objects from parent Activity

I have an ActionBarActivity that has a ViewPager with two Fragments and tabs created with ActionBar from the support library. My ActionBarActivity has also two buttons: cancel and save. When the save button is pressed, I need to get data from both of my Fragments. How do I do this? This data is stored fetched from the layout and stored in local variables in the Fragments.
You should be able get access to your fragments by use of FragmentManager by id or tag. getFragmentManager().findFragmentById() or getFragmentManager().findFragmentByTag()

Categories