title: Storing data in a changing listfragment
body: <p>I have a list fragment where each item contains a checkbox, and the user is able to check and uncheck it. Each item in the list fragment corresponds to an object in the class Task, which stores information on whether the checkbox is checked or not. </p>
  
  <p>My question is, how do I modify the Task object in response to the checking and unchecking of the checkbox? I know that you usually create a Task object and then notify the adapter so that the UI changes in response, but how do you reverse the process?</p>
  
  <p>List Fragment:</p>
  
  <pre><code>public class ListViewDemoFragment extends ListFragment {
  private static List&lt;Task&gt; mItems;        // ListView items list
  private ListViewDemoAdapter mAdapter;
  private IntentFilter intfil;
  private DataBaseWrapper db;
  
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      mItems = new ArrayList&lt;Task&gt;();
      intfil = new IntentFilter("CUSTOM");
      db = new DataBaseWrapper(getActivity());
      mItems = db.getAllTasks();
      mAdapter = new ListViewDemoAdapter(getActivity(), mItems);
      setListAdapter(mAdapter);
      getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
          @Override
          public boolean onItemLongClick(AdapterView&lt;?&gt; arg0, View arg1,
                                         int arg2, long arg3) {
              mItems.remove(arg2);
              mAdapter.notifyDataSetChanged();
              return true;
          }
      });
  }
  
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
      // remove the dividers from the ListView of the ListFragment
      getListView().setDivider(null);
  }
  
  @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
      // retrieve theListView item
      Task item = mItems.get(position);
      Toast.makeText(getActivity(), item.title, Toast.LENGTH_SHORT).show();
  }
  
  private BroadcastReceiver b = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          String name = intent.getStringExtra("name");
          String dline = intent.getStringExtra("dline");
          int pri = intent.getIntExtra("pri", 2);
          //String des = intent.getStringExtra("des");
          Task t = new Task(pri, name, dline, false);
          mItems.add(t);
          mAdapter.notifyDataSetChanged();
          db.addTask(t);
      }
  };
  
  
  public void onResume() {
      super.onResume();
      getActivity().registerReceiver(b, intfil);
  }
  
  public void onDestroy() {
      super.onDestroy();
      getActivity().unregisterReceiver(b);
  }
  
  public void onPause() {
      super.onPause();
      db.close();
  }
  
  public List&lt;Task&gt; getlist() {
      return mItems;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Adapter:</p>
  
  <pre><code>public class ListViewDemoAdapter extends ArrayAdapter&lt;Task&gt; {
  
  public ListViewDemoAdapter(Context context, List&lt;Task&gt; items) {
      super(context, R.layout.listview_item, items);
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      final ViewHolder viewHolder;
  
      if (convertView == null) {
          // inflate the GridView item layout
          LayoutInflater inflater = LayoutInflater.from(getContext());
          convertView = inflater.inflate(R.layout.listview_item, parent, false);
  
          // initialize the view holder
          viewHolder = new ViewHolder();
          viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
          viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
          viewHolder.tvDescription = (TextView) convertView.findViewById(R.id.tvDescription);
          viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
          convertView.setTag(viewHolder);
      } else {
          // recycle the already inflated view
          viewHolder = (ViewHolder) convertView.getTag();
      }
  
      // update the item view
      Task item = getItem(position);
      Drawable d = null;
      switch (item.icon) {
          case 0:
              d = ContextCompat.getDrawable(getContext(), R.drawable.red);
              break;
          case 1:
              d = ContextCompat.getDrawable(getContext(), R.drawable.yellow);
              break;
          case 2:
              d = ContextCompat.getDrawable(getContext(), R.drawable.green);
              break;
      }
      viewHolder.ivIcon.setImageDrawable(d);
      viewHolder.tvTitle.setText(item.title);
      viewHolder.tvDescription.setText(item.deadline);
      viewHolder.checkbox.setChecked(item.check);
      return convertView;
      }
  
              /**
               * The view holder design pattern prevents using findViewById()
               * repeatedly in the getView() method of the adapter.
               */
      private static class ViewHolder {
      ImageView ivIcon;
      TextView tvTitle;
      TextView tvDescription;
      CheckBox checkbox;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Task class (item in list)</p>
  
  <pre><code>public Task(int icon, String title, String deadline,boolean check) {
      this.icon = icon;
      this.title = title;
      this.deadline = deadline;
      this.check = check;
  }
  
  
  public String getName()
  {return title;}
  
  public int getIcon()
  {return icon;}
  
  public String getDeadline()
  {return deadline;}
  
  public boolean getCheck()
  {return check;}
  
  public void setCheck(Boolean b )
  {
      check = b;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Main Activity</p>
  
  <pre><code>public class MainActivity extends FragmentActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  }
  
  public void add(View view) {
      startActivityForResult(new Intent(MainActivity.this, TaskInput.class), 1);
  }
  
  public void save(View view)
  {
      DataBaseWrapper db = new DataBaseWrapper(this);
      db.deleteAll();
      ListViewDemoFragment f = (ListViewDemoFragment)getFragmentManager().findFragmentById(R.id.frag);
      for (Task item: f.getlist())
      {
          db.addTask(item);
      }
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data)
  {
      super.onActivityResult(requestCode, resultCode, data);
      if(requestCode==1)
      {
         sendBroadcast(data);
      }
  }
  </code></pre>