Code example for ListFragment

Methods: getListView, onViewCreated, setListAdapter, setRetainInstance

0
 
    } 
 
    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
 
        // Restore the previously serialized activated item position. 
        if (savedInstanceState != null
                && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
            setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
        } 
         
        ArrayList<Student> mStudents = mPeriod.getmStudents();
        StudentAdapter adapter = new StudentAdapter(mStudents);
        // TODO: replace with a real list adapter. 
        setListAdapter(adapter);
        setRetainInstance(true);
    } 
 
    @Override 
    public void onAttach(Activity activity) {
        super.onAttach(activity);
 
        // Activities containing this fragment must implement its callbacks. 
        if (!(activity instanceof Callbacks)) {
            throw new IllegalStateException("Activity must implement fragment's callbacks.");
        } 
 
        mCallbacks = (Callbacks) activity;
    } 
 
    @Override 
    public void onDetach() { 
        super.onDetach(); 
 
        // Reset the active callbacks interface to the dummy implementation. 
        mCallbacks = null;
    } 
     
    public void updateUI() { 
        ((StudentAdapter)getListAdapter()).notifyDataSetChanged();
    } 
 
    @Override 
    public void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);
         
        Student s = ((StudentAdapter)getListAdapter()).getItem(position);
 
        // Notify the active callbacks interface (the activity, if the 
        // fragment is attached to one) that an item has been selected. 
        mCallbacks.onItemSelected(s);
    } 
 
    @Override 
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mActivatedPosition != ListView.INVALID_POSITION) {
            // Serialize and persist the activated item position. 
            outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
        } 
    } 
 
    /** 
     * Turns on activate-on-click mode. When this mode is on, list items will be 
     * given the 'activated' state when touched. 
     */ 
    public void setActivateOnItemClick(boolean activateOnItemClick) {
        // When setting CHOICE_MODE_SINGLE, ListView will automatically 
        // give items the 'activated' state when touched. 
        getListView().setChoiceMode(activateOnItemClick
                ? ListView.CHOICE_MODE_SINGLE
                : ListView.CHOICE_MODE_NONE);
    } 
 
    private void setActivatedPosition(int position) {
        if (position == ListView.INVALID_POSITION) {
            getListView().setItemChecked(mActivatedPosition, false);
        } else { 
            getListView().setItemChecked(position, true);
        } 
 
        mActivatedPosition = position;
    }