6

I have two fragments on a view pager. I once had to move data from fragment B to A and refresh the data displayed on A and I did it with getItemPosition. For some reason, the same method doesn't work when I try to reset all data..

In my adapter i have :

public void refresh() { notifyDataSetChanged(); } @Override public int getItemPosition( Object obj ) { return POSITION_NONE; } 

in fragment where I click 'reset' :

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); notTriedPasswordsList = PagerActivity.mainList; ..... .... resetButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick( View v ) { PagerActivity.resetPasswords(); PagerActivity.viewPagerAdapter.refresh(); }}); 

viewPager activity hosting both fragments:

public static void resetPasswords() { mainList.addAll( 0, historyList ); historyList.clear(); PagerActivity.viewPagerAdapter.refresh(); } 

Main fragment where the pass is displayed :

@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) { View view = inflater.inflate(R.layout.fragment_main, container, false); ..... nextCodeDisplay = ( TextView ) view.findViewById( R.id.passwordDisplayTextView ); nextCodeDisplay.setText( notTriedPasswordsList.get( 0 ).getPasswordString() ); .... nextButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { notTriedPasswordsList.remove( 0 ); if( notTriedPasswordsList.size() > 0 && !(notTriedPasswordsList.get( 0 ).getTried()) ) { nextCodeDisplay.setText( notTriedPasswordsList.get( 0 ).getPasswordString() ); } } 
2
  • How is your progress? I notice you have not responded recently. Commented Jul 29, 2015 at 17:58
  • use interface for communication between two fragments, it will work in that way..... Commented Jul 30, 2015 at 3:23

4 Answers 4

2
+25

PagerActivity is treated like a static class, and you can only access static methods and member data and objects in this way. About code:

notTriedPasswordsList = PagerActivity.mainList; 

Note: So now PagerActivity can access static mainList object, or notTriedPasswordsList (sharing the same memory). But this is the only object you can access since your code references static methods.

On code PagerActivity.viewPagerAdapter.refresh(), I am not clear on what data this refreshes since I don't see the enough code, again refresh() must be a static method. With code notifyDataSetChanged(), there must be a direct link between viewPagerAdapter and the data object, probably an ArrayList. Certainly I don't see any direct relation between the two.

Perhaps you want code like:

viewPagerAdapter pagerAdapter = new viewPagerAdapter(); 

This way you can have the relationship between the adapter and possibly an ArrayList object. The benefit of creating an instance with new is that it saves data and the state inside the class in the form of an object, in my sample that is pagerAdapter.

I could not suggest specific set of codes for now since I don't see sufficient amount of it for me to fix. Perhaps you can fix code first and then we all can contribute.

Sign up to request clarification or add additional context in comments.

1 Comment

@BVtp, For clarity, I think you should post more relevant code related to the Adapter.
1

Your call to PagerActivity.viewPagerAdapter.refresh(); won't cause your fragment to be redrawn. Instead you should access your fragment directly and create a custom refreshUI() method in it.

public void refreshUI(){ nextCodeDisplay.setText( notTriedPasswordsList.get( 0 ).getPasswordString() ); } 

6 Comments

can you even call clear on an adapter? I'm getting an error.. the method getAdapter() is undefined for the type ...
What are mainList and historyList (ListView ? ArrayList?)? What do you mean by reset data ? clear the list only or clear and refill with default items ?
they're both array lists. By resetting I am deleting everything in historyList , and moving what was in there back to mainList which is what is displayed to the user.
Then your code seems to be only modifying data but never the UI. can you post the code where you display your password for the first time ?
I've edited the original post and added it there . Thank you.
|
1

I suggest to change your approach. I've uploaded a simple project to my dropbox public folder. Here you can find a reference implementation of how two fragments managed by a ViewPager can share information. The first Fragment - Fragment#1 - simply displays a String that is generated by Fragment#2. Fragment#2 has a button that, when clicked, sends a random String to Fragment#1 through the Activity. No need to refresh viewpager, no need of static methods, simple and working. I guess you can adapt this example to your needs.

Comments

0

As you said that you want to refresh your data, personally i would like to suggest to use swipe refresh layout. It will be very useful for this purpose and stylish as well. Following is the code.

Swipe_Refresh_layout.xml

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipe_container" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" /> </android.support.v4.widget.SwipeRefreshLayout> 

And following is the activity i am using for this layout.

public class LatestNewsFragment extends Fragment implements OnRefreshListener ,OnScrollListener{ SwipeRefreshLayout swipeLayout; public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { = View rootView = inflater.inflate(R.layout.Swipe_Refresh_layout, container, false); swipeLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container); swipeLayout.setOnRefreshListener(this); swipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); return rootView; } public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { swipeLayout.setRefreshing(false); additemstatus(); } }, 5000); } 

Now in overridden refresh method you can refresh or load your data. I hope this will be very helpful for your support.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.