I imported the library [1]: https://github.com/shontauro/android-pulltorefresh-and-loadmore
Example class was extending ListActivity
and all works.
I redid on extends ListFragment
and there was an error.
public class SurveyGuestsList extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.loadmore, null);
arrayList = new ArrayList<SurveyGuestsListBean>();
context = getActivity();
new LoadDataTask().execute();
// set a listener to be invoked when the list reaches the end
((LoadMoreListView) getListView())
.setOnLoadMoreListener(new OnLoadMoreListener() {
public void onLoadMore() {
// Do the work to load more items at the end of list
// here
new LoadDataTask().execute();
}
});
return v;
}
ERROR:
11-18 07:34:10.508 1777-1777/com.example.MANAGMENT E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at com.example.MANAGMENT.fragments.SurveyGuestsList.onCreateView(SurveyGuestsList.java:50)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
EDITE: I moved the code here and it worked !!!
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
((LoadMoreListView) getListView())
.setOnLoadMoreListener(new OnLoadMoreListener() {
public void onLoadMore() {
// Do the work to load more items at the end of list here
new LoadDataTask().execute();
}
});
super.onViewCreated(view, savedInstanceState);
}
Fragments and Activities have different life-cycles and call backs. For fragments, onCreateView is for creating the view it is not attached to the view hierarchy until you return v
, so you cannot call getListView
in it. Just create and return your view as follows.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.loadmore, null);
return v;
}
Move the rest of your code to onViewCreated()
or a later callback.