Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing data display on a fragment

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() );
                        }
                    }   
like image 274
BVtp Avatar asked Jul 22 '15 13:07

BVtp


People also ask

How do I refresh a ViewPager fragment?

OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager. addOnPageChangeListener(new ViewPager.

How do I refresh a fragment from another fragment?

So when you click your Button , find the Fragment you want to refresh by its tag, and then call your refresh method.

How do you refresh a fragment after onBackPressed from activity?

As per me there is simple to refresh the fragment when come back from other fragment, We just have to override the onActivityCreated Method for refresh fragment. Show activity on this post. You can also update/refresh the fragment using onStart() method.


1 Answers

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.

like image 171
The Original Android Avatar answered Nov 06 '22 00:11

The Original Android