Tuesday 15 May 2012

android - update a listview in a fragment in a viewpager from activity -


I understand that this is a common problem, and I've mentioned many different questions but I still Not getting it to work.

My activity implements a view pager with two tabs, and each tab has a listview. I have an adapter for my view pager, in two pieces and each section, an adapter in the list view. Adds to the link.

In my activity menu, I have a menu that warns me for input in one of the list views in one of the warning lists for me. ... subAdapter = new sub-adapter (getSupportFragmentManager (), data); (ViewPager) findViewById (R.id.viewPager) setAdapter (subGroupAdapter); }

My viewfapper adapter

  public class sub-adapter FragmentPagerAdapter {public SubGroupAdapter (FragmentManager fm, data data) {super (fm); } @ Override Public Piece Mill ITIme (Entity Status) {Bundle Bundle = New Bundle (); Bundle.putString ("data", data); Switch (position) {Case 0: piece 1 frag1 = new piece 1 (); Frag1.setArguments (bundle); Return frag1; Case 1: Piece 2 frag2 = new piece 2 (); Frag2.setArguments (bundle); Return2; } Return tap; } // Below are some other methods  

piece 1 / piece 2 (both the pieces have a list view)

  operated on public zeroes (saved bundles) InstantState) {super.onActivityCreated (savedInstanceState); Frag1Adapter = New frag1Adapter (this, data); (ListView) getView () FindViewById (R.id.listView) setAdapter (frag1Adapter); }  

Custom list view adapter for both list view in both pieces apply

  public class ExpenseAdapter BaseAdapter OnClickListener {...}  

As I mentioned earlier, I can input a new entry in the list view from the Activity Action Bar button. However, the list view can not be updated and I can not provide a reference to notify the inventory adapter to inform the activity ().

What is the best way to move from here? Thank you very much!

I tried to use the interface, tag but it can not get it to work currently.

What you should do is create a public way like your Fragment 1 and Fragment 2:

Define in your activity:

Fragment1 frag1; Fragment2 frag2;

Then your ViewPager :

  public class sub-adapter accesses FragmentPagerAdapter {public sub-group adapter (fragment manager FM) , Data data) {super (FM); } @ Override Public Piece Mill ITIme (Entity Status) {Bundle Bundle = New Bundle (); Bundle.putString ("data", data); Switch (position) {case 0: frag1 = new piece 1 (); Frag1.setArguments (bundle); Return frag1; Case 1: frag2 = new piece 2 (); Frag2.setArguments (bundle); Return2; } Return tap; }}  

Place this method in Fragment1.java:

  public zero updateFragment1ListView () {if (adapter! = Null) {adapter.notifyDataSetChanged ( ); }}  

and from your activity call:

  if (frag1! = Null) {frag1.updateFragment1ListView (); }  

Obviously the name of your adapter is changed if it is not called an adapter ...

Do the same for basil

< / Div>

No comments:

Post a Comment