title: Error: Attempted to access a cursor after it has been closed?
body: <p>In my application I'm fetching <code>inbox messages</code> from native application to <code>my application</code>. Also there are checkboxes to select multiple messages.But the problem arises are no messages are displaying in <code>listview while running code</code> and also showing error <code>android.database.StaleDataException: Attempted to access a cursor after it has been closed.
  </code></p>
  
  <pre><code>ArrayAdapter&lt;SMSListModel&gt; adapter;
  List&lt;SMSListModel&gt; list = new ArrayList&lt;SMSListModel&gt;();
  TextView textViewSMSSender, textViewSMSBody
  
   public void getInboxSms() {
      //Uri message = Uri.parse("content://sms/inbox");
      ContentResolver cr = getContentResolver();
      c = cr.query(Uri.parse("content://sms/inbox"), null, null, null, null);
      //startManagingCursor(c);
       //totalSMS = c.getCount();
      if (c.moveToFirst()) {
          for (int i = 0; i &lt; c.getCount(); i++) {
  
              Log.d("SMSss", "Contact number : "+ c.getString(c.getColumnIndexOrThrow("address"))+ "\n"
                              + "msg : " + c.getColumnIndexOrThrow("body")
                              + "\n"
                              + "Person : "
                              + getContactName(getApplicationContext(),c.getString(c.getColumnIndexOrThrow("address"))));
              c.moveToNext();
          }
      }
     c.close();
  
  }
  
  public String getContactName(Context context, String phoneNumber) {
      ContentResolver cr = context.getContentResolver();
      Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
              Uri.encode(phoneNumber));
      Cursor cursor = cr.query(uri,
              new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
      if (cursor == null) {
          return null;
      }
      String contactName = null;
      if (cursor.moveToFirst()) {
          contactName = cursor.getString(cursor
                  .getColumnIndex(PhoneLookup.DISPLAY_NAME));
      }
      if (cursor != null &amp;&amp; !cursor.isClosed()) {
          cursor.close();
      }
      return contactName;
  }
  private List&lt;SMSListModel&gt; getModel() 
  {
      if(c.getCount()&gt;0)
      {
          for(int i=0;i&lt;c.getCount();i++)
          {
              if(c.moveToPosition(i))
              {                   
                  list.add(new SMSListModel(c.getString(c.getColumnIndex("address")),c.getString(c.getColumnIndex("body"))));
  
              }
          }
      }
  
      return list;
  
  }
  </code></pre>
  
  <p>SmsListAdapter</p>
  
  <pre><code>public class SMSListAdapter extends ArrayAdapter&lt;SMSListModel&gt; {
  
  private final List&lt;SMSListModel&gt; list;
  private final Activity mContext;
  boolean checkAll_flag = false;
  boolean checkItem_flag = false;
  
  public SMSListAdapter(Activity context,List&lt;SMSListModel&gt; list) 
  {
      super(context, R.layout.listview_each_item, list);
      Log.d("size of list", ""+list.size());
      mContext = context;
      this.list = list;
  
  }
  
  static class ViewHolder {
      protected TextView textAddress;
      protected TextView textBody;
      protected CheckBox checkbox;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
  
      ViewHolder viewHolder = null;
      if (convertView == null) {
          LayoutInflater inflator = mContext.getLayoutInflater();
          convertView = inflator.inflate(R.layout.listview_each_item, null);
          viewHolder = new ViewHolder();
          viewHolder.textAddress = (TextView) convertView.findViewById(R.id.tvSMSSend);
          viewHolder.textBody = (TextView) convertView.findViewById(R.id.tvSMSBody);
          viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.cbSelect);
          viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
                      @Override
                      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                          int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                          list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                      }
                  });
          convertView.setTag(viewHolder);
          convertView.setTag(R.id.tvSMSSend, viewHolder.textAddress);
          convertView.setTag(R.id.tvSMSBody, viewHolder.textBody);
          convertView.setTag(R.id.cbSelect, viewHolder.checkbox);
      } else {
          viewHolder = (ViewHolder) convertView.getTag();
      }
      viewHolder.checkbox.setTag(position); // This line is important.
      viewHolder.textAddress.setText(list.get(position).getAddress());
      viewHolder.textBody.setText(list.get(position).getBody());
      viewHolder.checkbox.setChecked(list.get(position).isSelected());    
      return convertView;
  }}
  </code></pre>
  
  <p>SMSListModel</p>
  
  <pre><code>public class SMSListModel {
  
  private String address;
  String body;
  private boolean selected;
  
  public SMSListModel(String address, String body) {
      this.address = address;
      this.body = body;
  }
  
  public String getAddress() {
      return address;
  }
  
  public String getBody() {
      return body;
  }
  
  public boolean isSelected() {
      return selected;
  }
  
  public void setSelected(boolean selected) {
      this.selected = selected;
  }}
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/tylBI.png" alt="Logcat"></p>