Felix
Felix

Reputation: 581

Force fragment in viewpager reload adapter

I have viewpager below: enter image description here

Each view will load a list view which is stored in sqlite. In each item of list view haves a button to change itself to other list by making a change in DB.
For example: i can move item in List fragment to Absen fragment or Atten fragment

The problem is that the view of the middle fragment not reload. I think because my adapter not call (because the fragment is in the middle so it already is called in previous fragment). My fragment:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list_student_current_session, container,false);

        ButterKnife.bind(this, view);

        setListViewAdapter();

        return view;
    }

    public void setListViewAdapter(){
        DatabaseHelper db = new DatabaseHelper(getActivity());
        list = db.getListStudentAbsen(((CurrentSessionActivity)this.getActivity()).getSessionID());

        listView.setAdapter(new ListAbsenStudentCurrentSessionAdapter(list, getActivity()));
    } 

Is this implement is correct or i should try use other to interact with DB such as only update db when the activity is totally finish.

Upvotes: 0

Views: 345

Answers (2)

Felix
Felix

Reputation: 581

i found the solution for my case by using this:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser && isResumed) {
        //call set adapter here
    }
}

Upvotes: 0

shagi
shagi

Reputation: 663

It's not clear what happens on in your program. But as I could understand Litst view item in first ViewPager item can update itselft and then it should appear in second ViewPager item? If it so you should get instance of second ViewPager item with pagerAdapter.getItem(position) and then call something like setData(list) -> notifyDataSetChanged()

For example you have 3 fragments in PagerAdapter each fragment has its own list view, and when data in first fragment's list updates you have to update second's fragment list adapter.

Upvotes: 1

Related Questions