Advertisement
Guest User

Untitled

a guest
Aug 8th, 2013
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. public class DetachableAdapter extends BaseAdapter {
  2.  
  3.     private Context mContext;
  4.     private List<String> mData;
  5.  
  6.     public DetachableAdapter() {
  7.         // no Context yet.
  8.     }
  9.  
  10.     public void setContext(Context context) {
  11.         mContext = context;
  12.     }
  13.  
  14.     public void setData(List<String> data) {
  15.         mData = data;
  16.         notifyDataSetChanged();
  17.     }
  18.  
  19.     @Override
  20.     public int getCount() {
  21.         return mData.size();
  22.     }
  23.  
  24.     @Override
  25.     public Object getItem(int location) {
  26.         return mData.get(location);
  27.     }
  28.  
  29.     @Override
  30.     public long getItemId(int location) {
  31.         return location;
  32.     }
  33.  
  34.     @Override
  35.     public View getView(int location, View convertView, ViewGroup root) {
  36.         // context can't be null here since this code is not exectued w/o context
  37.         LayoutInflater inflater = LayoutInflater.from(mContext);
  38.         View view = convertView;
  39.         if (view == null) {
  40.             view = inflater.inflate(android.R.layout.simple_list_item_1, null);
  41.         }
  42.         ((TextView) view).setText(mData.get(location));
  43.         return view;
  44.     }
  45. }
  46.  
  47. ///////////////////////////////////////////////////////////////
  48.  
  49. public class RetainedFragment extends ListFragment {
  50.  
  51.     // ok to keep since it does not keep the reference to context
  52.     private final DetachableAdapter mAdapter = new DetachableAdapter();
  53.     // ok to keep since it does not keep any references
  54.     private final List<String> mData = new ArrayList<String>();
  55.  
  56.     public RetainedFragment() {
  57.         for (int i = 0; i < 100; i++) {
  58.             mData.add("SomeData:" + i);
  59.         }
  60.     }
  61.  
  62.     @Override
  63.     public void onCreate(Bundle savedInstanceState) {
  64.         super.onCreate(savedInstanceState);
  65.         setRetainInstance(true);
  66.         setListAdapter(mAdapter);
  67.     }
  68.  
  69.     @Override
  70.     public void onAttach(Activity activity) {
  71.         super.onAttach(activity);
  72.         mAdapter.setContext(activity);
  73.     }
  74.  
  75.     @Override
  76.     public void onDetach() {
  77.         super.onDetach();
  78.         mAdapter.setContext(null);
  79.     }
  80.  
  81.     @Override
  82.     public void onResume() {
  83.         super.onStart();
  84.         updateList();
  85.     }
  86.  
  87.     private void updateList() {
  88.         // simulate some sort of updates probably due to listeners..
  89.         Collections.shuffle(mData);
  90.         mAdapter.setData(mData);
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement