Question

I have an Activity which holds a ViewPager with 2 Fragments. One fragment is for adding items to ListView and another fragment is holding the ListView.

I've been trying for almost 3 days now without any positive results. How do I update the other fragment's ListView from the first fragment?

I'm trying to call the method that updates ListView from the Activity that holds ViewPager but it doesn't work.

Calling the method from ViewPager activity :

@Override
    public void onPageSelected(int position) {
    library.populateListView(getApplicationContext());
    aBar.setSelectedNavigationItem(position);
}

This is the populateListView method:

public void populateListView(Context context){      
    CustomListViewAdapter customAdapter = new CustomListViewAdapter(getDatabaseArrayList(context), getActivity());

        if (lView != null)
        {
            lView.setAdapter(customAdapter);
        }
        customAdapter.notifyDataSetChanged();
}

However this doesn't work because the lView variable (ListView) is null because the fragment isn't shown at the moment when this is being called.

Was it helpful?

Solution

I am assuming that function populateListView() is a function of the Fragment containing the ListView. You are calling populateListView() on every call to onPageSelected. Should you not check what is the position that is being selected. Anyway the populateListView() method should be a public method of the Fragment containing ListView. And You Can Instantiate The Fragment from the Viewpager adapter in the Activity and than call this method. In That way the listView should not be null.

@Override
public void onPageSelected(int position) {
ListViewFragment frag=(ListViewFragment)adapter.instantiateItem(viewPager, 1);
//here adapter is the ViewPager Adapter and you must supply the viewpager that    contains 
//the fragments and also the position of the fragment to instantiate. 
//For example 0 or 1 etc.
frag.populateListView(getApplicationContext());
aBar.setSelectedNavigationItem(position);
}

OTHER TIPS

Understand Fragments

Please see this link. I have gone in great detail explaining the concept of fragments.

Pay particular attention to the definition of rootview:

public void onActivityCreared(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
            // Do stuff on creation. This is usually where you add the bulk of your code. Like clickListners
        // You can define this object as any element in any of your xml's 
        View rootview = inflater.inflate(R.layout.xml_the_fragment_uses container,false);
        rootview.findViewById(R.id.your_id).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Do something 
        }
    });
} 

In the above case I defined a button for an on click listener, but you can just as easily define a ListView along with its appropriate methods.

Alternate Solution

A second method could be using getView or getActivity (check the communicating with activity section).

For example:

ListView listView = (ListView) getView().findViewById(R.id.your_listView_id);

OR    (more likely solution for your problem)

View listView = getActivity().findViewById(R.id.list);

Please read this post for additional information.

Good Luck.

To do this safely, you have to keep your listview data in a higher level Parent-Activity not the fragments. Make your MainActivityclass singleton class by making the constructor private and create a getInstance method that return the only initialized instance of your `MainActivity.

This will allow you to keep your data instance safe from being re-initialized or lost. Then, in onResume of your fragment re-set the data (get it from the MainActivity) to your listview adapter and call notifyDataSetChanged() method from the adapter instance.

This will do the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top