Unable to update data in views in fragment - java

I have a view pager that returns same fragment with different data from adapter every time i swap but despite of the fact that different data is received it displaying the same data over and over again, how can i make fragment update its data
the structure is, there is a fragment that has a listview and when i click on the listview item it replaces a fragment that has viewpager and setting its adapter, the adapter is FragmentPagerAdapter and returns a fragment with argument now fragment uses its argument and display data, every time getview called of adapter returning the same fragment with different data
here is the code:
when list item clicked:
replaceFragment(ReportPaginationFragment.newInstance(i, isFromList), true, true);
ReportPaginationFragment:
viewPager.setAdapter(new ReportPaginationAdapter(getFragmentManager(), reportID, appointmentObjectArrayList, isFromList, listPosition ));
Adapter:
reportId = appointmentObjectArrayList.get(item++).getId();
return NewDrReportFragment.newInstance(reportId);
appointmentObjectArrayList is arraylist that has data
Thanks in advance

Instead of addFragment, replace is better choice.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();

for (int i=0;i<categoryList.size();i++){
Category category=categoryList.get(i);
FoodFragment foodFragment = new FoodFragment();
foodFragment.setCategory(category);
pagerAdapter.addFragment(foodFragment, category.getName());
}
lets take this example. Maybe you are not setting differrent data onto pageAdapter properly.
can you share your code for more information?

Related

Replace a Fragment with another from a separate java class

I have a Recycler View in a Fragment that I have an adapter class for in a separate file. I am trying to make it so that when you click on a RecyclerView Item it replaces the current fragment with a new one. The code to replace a fragment with another that I had been using previously is this:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.nav_host_fragment, NewFragment.class, null);
transaction.addToBackStack("CurrentFragment");
transaction.commit();
And this works inside the fragment, but since I have to call it from the java class I tried this instead:
FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.nav_host_fragment, NewFragment.class, null);
transaction.addToBackStack("CurrentFragment");
transaction.commit();
And this just layers the new fragment on top of the current one. If I try making a class in the Current Fragment with the transaction code in it and call it, it doesn't work because of the static context.
Is there any way to do this?

Adding a fragment to RecyclerView

I have an app with functionality to show one record or a list of them. I have a fragment for single record, so I decided for a list I want to fill a RecyclerView with cards, and add a fragment into each card. I am getting IllegalArgumentException: Cannot add a null child to a ViewGroup on a line in onBindViewHolder():
holder.frameLayout.addView(ResultFragment.newInstance(id).getView())
I'm not so sure how to work with a Recycled View and Fragments but if you use a linearLayout or RelativeLayout, you can use FragmentManager in the onCreateView method or generate you're pwn method to change the content in the view (fragment) that you want to change:
FragmentManager fm = getSupportFragmentManager();
transaction = fm.beginTransaction();
oneFragment = new OneFragment();
transaction.replace(R.id.fragmentContent, oneFragment);
transaction.commit();
the R.id.fragmentManager are a <fragment> section in your view
Regards

Completely Refresh Fragment

I have a fragment and I need to completely refresh/reinstantiate it.
By refreshing I mean recreating fragment.
I have tried using FragmentManager with detach() method but it didn't help. All EditText children of that fragment still have their values entered even though the fragment was refreshed
Is there any way to achieve this result?
if there are only edit texts , create a method that sets all edit texts to empty string that is et(edit_text object),
et.setText(""); //edit text will be reset
Put the following code inside the fragment which need to be refreshed.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
Use fragment manger according to your code (if it is an old version then this will be supportFragmentManager)
Put and call this method to your fragment
private void reloadFragment(){
Fragment frg = getActivity().getSupportFragmentManager().findFragmentByTag(ReceiptFragment.TAG);
final FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}

Why the RecyclerView items disappear when the transition animation start from one fragment to another?

I have a recyclerview items inside a fragment when I click on an item from the recyclerview it repleces the olde fragment with a new one. And i have an animation going when the transition happens like this:
FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_out_top,R.anim.slide_in_top);
ft.replace(R.id.container, AnotherFragment.newInstance());
ft.comit;

understanding Fragment replacement using FragmentPagerAdapter

I'm having a hard time understanding how to replace fragments using the FragmentPagerAdapter. I have a class that extends FragmentPagerAdapter (android.support.v13.app.FragmentPagerAdapter) and my MainActivity implements ActionBar.TabListener, in the FragmentPageAdapter class I use the getItem() to setup my 3 fragments. so far so good.
One of these fragments is a ListView (pos #1), which I use a Listener to check on the onItemClick().
I want to replace the current ListView Fragment with another fragment so what I did is that inside the onItemClick() I perform the following code:
FragmentManager mFragmentManager = mActivity.getFragmentManager();
//new fragment to replace
Fragment mFragment = new TabPlaceFragment();
mFragmentManager.beginTransaction()
.replace(R.id.pager, mFragment)
.addToBackStack(null)
.commit();
What this is doing is that the fragment is replace with a blank fragment, I'm assuming is the pager, but the mFragment does not get replace. I tried giving an Id to the Current fragment and tried replacing it.
mFragmentManager.beginTransaction()
.replace(R.id.frament_old, mFragment)
.addToBackStack(null)
.commit();
But what this does is that it overlaps the new fragment on top of the old fragment
finally I tried getting the Id of the fragment to replace with the new, but this also gave me a blank fragment.
//this gives me the current fragment I want to replace
Fragment mCurrent = mFragmentManager.findFragmentByTag(MainHelper.getFragmentName(mViewPager.getId(), 1));
mFragmentManager.beginTransaction()
.replace(mCurrent.getId(), mFragment)
.addToBackStack(null)
.commit();
I'm sure I'm not implementing the method correctly but I cannot find a good example to follow the logic of my code. I thought of putting the fragment replacement in the MainActivity under getItem(), but don't know how to call it from within my onItemClick() Listener.
Any help is highly appreciated!!!
Thanks.

Categories