Code example for ListFragment

Methods: getActivity, onResume, setListAdapter

0
    setHasOptionsMenu(true);
  } 
   
  @Override 
  public void onResume() { 
    super.onResume(); 
     
    helper=new RestaurantHelper(getActivity());
    prefs=PreferenceManager.getDefaultSharedPreferences(getActivity());
    initList(); 
    prefs.registerOnSharedPreferenceChangeListener(prefListener);
  } 
   
  @Override 
  public void onPause() { 
    helper.close();
 
    super.onPause(); 
  } 
   
  @Override 
  public void onListItemClick(ListView list, View view,
                              int position, long id) {
    if (listener!=null) {
      listener.onRestaurantSelected(id);
    } 
  } 
   
  @Override 
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.option, menu);
  } 
 
  @Override 
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()==R.id.add) {
      startActivity(new Intent(getActivity(), DetailForm.class));
       
      return(true); 
    } 
    else if (item.getItemId()==R.id.prefs) {
      startActivity(new Intent(getActivity(), EditPreferences.class));
     
      return(true); 
    } 
 
    return(super.onOptionsItemSelected(item));
  } 
   
  public void setOnRestaurantListener(OnRestaurantListener listener) {
    this.listener=listener;
  } 
   
  private void initList() { 
    if (model!=null) {
      model.close();
    } 
     
    model=helper.getAll(prefs.getString("sort_order", "name"));
    adapter=new RestaurantAdapter(model);
    setListAdapter(adapter);
  } 
   
  private SharedPreferences.OnSharedPreferenceChangeListener prefListener=
   new SharedPreferences.OnSharedPreferenceChangeListener() { 
    public void onSharedPreferenceChanged(SharedPreferences sharedPrefs, 
                                          String key) { 
      if (key.equals("sort_order")) { 
        initList();