Updating data display on a fragment

I have two fragments on the pager. I once had to move data from fragment B to and update the data displayed on A, and I did this with getItemPosition. For some reason, the same method does not 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 the snippet 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 containing both fragments:

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

The main fragment on which 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() ); } } 
+6
source share
4 answers

PagerActivity considered a static class, and you can access static methods and element data and objects in this way. About the code:

 notTriedPasswordsList = PagerActivity.mainList; 

Note: PagerActivity can now access the static mainList or notTriedPasswordsList (share the same memory). But this is the only object you can access as your code refers to static methods.

In the PagerActivity.viewPagerAdapter.refresh() code, I don’t understand what data is being updated, since I do not see enough code, refresh() again should be a static method. With the notifyDataSetChanged () code, notifyDataSetChanged must be a direct relationship between the viewPagerAdapter and the data object, possibly an ArrayList . Of course, I do not see a direct connection between them.

Perhaps you need code like:

 viewPagerAdapter pagerAdapter = new viewPagerAdapter(); 

This way you can establish a connection between the adapter and possibly an ArrayList object. The advantage of creating an instance with a new one is that it saves data and state inside the class in the form of an object in my pagerAdapter example.

I could not offer a specific set of codes, since I do not see enough of it to fix it. Perhaps you can fix the code first, and then we can all contribute.

+2
source

Your call to PagerActivity.viewPagerAdapter.refresh(); will not redraw the fragment. Instead, you should access your fragment directly and create your own refreshUI() method in it.

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

I suggest changing your approach. I uploaded a simple project to my Dropbox shared folder . Here you can find a reference implementation of how the two fragments that ViewPager manages can exchange information. The first fragment - fragment # 1 - simply displays the line that is generated by fragment # 2. Fragment No. 2 has a button that, when clicked, sends a random line to fragment # 1 through Activity. No need to update the viewpager, no static methods, simple and working. I think you can adapt this example to your needs.

+1
source

As you said that you want to update your data, I personally would like to suggest using the layout update wipes. It will be very useful for this purpose and stylish. Below 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 the next activity that I use 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 the overridden update method, you can update or upload your data. Hope this will be very helpful for your support.

0
source

All Articles