Search code examples
androidandroid-fragmentsactionbarsherlock

How to use Activity code in SherlockFragment


I have written some activities in which i am fetching data into ListView using JSON, but now i have to use Navigation Drawer in my app using Sherlock Fragment, and I have made Fragments to use and simply showing text message in each and every Fragment.

Fragment1.java:

public class Fragment1 extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment1, container, false);
        return rootView;
    }

}

fragment1.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/Fragment1" />

</RelativeLayout>

But now i need to use my existing code in which i used Activity, in SherlockFragment, So here i want to know what are the changes i need to do in my code ?

For an example how to use this tutorial code in SherlockFragment:

http://www.androidhive.info/2012/10/android-multilevel-listview-tutorial/


Solution

  • Follow these steps

    1. inflate layout in onCreateView()

    2. get references of all views using findViewById() in onActivityCreated()

    3. start JSON Parsing in onCreate() or wherever you want

    4. populate ListView after fetching data from JSON.