title: CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 happens when record is deleted
body: <p>A i understand in something with a cursor, but i can't figure out a solution for my own. So I need your help.
  And it happens only when a delete a record. On DisplayContact.java in line 65.</p>
  
  <p>Here is my class files.</p>
  
  <p><strong>DisplayContact.java</strong></p>
  
  <pre><code>import java.text.SimpleDateFormat;
  import java.util.Calendar;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.view.KeyEvent;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class DisplayContact extends Activity {
  
       int from_Where_I_Am_Coming = 0;
         private DBHelper mydb ;
         TextView name ;
         TextView note;
         int id_To_Update = 0;
  
         Button btnDel;
  
         String formattedDate;
         TextView txtView;
         SimpleDateFormat date;
         Calendar c;
  
  //     TextView dosage;
  //     TextView day;
  
         @Override
         protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_display_contact);
  
            name = (TextView) findViewById(R.id.editTextName);
            note = (TextView) findViewById(R.id.editTextNote);
  
            btnDel = (Button) findViewById(R.id.buttonDelete);
  
            mydb = new DBHelper(this);
  
            c = Calendar.getInstance();
            date = new SimpleDateFormat("yyyy-MM-dd");
            formattedDate = date.format(c.getTime());
            txtView = new TextView(this);
  
            Bundle extras = getIntent().getExtras(); 
            if(extras !=null)
            {
               int Value = extras.getInt("id");
               if(Value&gt;0){
                  //means this is the view part not the add contact part.
                  Cursor rs = mydb.getData(Value);
                  id_To_Update = Value;
                  rs.moveToFirst();
                  String nam = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_NAME));
                  String not = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_NOTE));
                  String cd = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_DATE));
  
                  if (!rs.isClosed()) {
                     rs.close();
                  }
  
                  name.setText((CharSequence)nam);
                  note.setText((CharSequence)not);
                  txtView.setText(formattedDate);            
                 }
  ////////////////////////////////////////////////////////////////////////////////////////////
                 OnClickListener listenerDel = new OnClickListener() {
                     public void onClick(View v) {
  
                      mydb.deleteContact(id_To_Update);
                      Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();  
                      Intent intent = new Intent(getApplicationContext(),com.example.database3.MainActivity.class);
                      startActivity(intent);
                      finish();
                     }
  
                 };
  
                 btnDel.setOnClickListener(listenerDel);
            }
         }
         @Override
         public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            Bundle extras = getIntent().getExtras(); 
            if(extras !=null)
            {
               int Value = extras.getInt("id");
               if(Value&gt;0){
                  getMenuInflater().inflate(R.menu.display_contact, menu);
               }
               else{
                  getMenuInflater().inflate(R.menu.mainmenu, menu);
               }
            }
            return true;
         }
  
         public void run(View view)
         {    
            Bundle extras = getIntent().getExtras();
            if(extras !=null)
            {
               int Value = extras.getInt("id");
               if(Value&gt;0){
                  if(mydb.updateContact(id_To_Update,name.getText().toString(),         
                      note.getText().toString(),
                      txtView.getText().toString())){
  
                         Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();   
                         Intent intent = new Intent(getApplicationContext(),com.example.database3.MainActivity.class);
                         startActivity(intent);
                  }       
                  else{
                     Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();   
                  }
               }
               else{
                  if(mydb.insertContact(name.getText().toString(),
                      note.getText().toString(),
                      txtView.getText().toString())){
                          Toast.makeText(getApplicationContext(), "Added", Toast.LENGTH_SHORT).show();    
                  }       
                  else{
                     Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();  
                  }
                  Intent intent = new Intent(getApplicationContext(),com.example.database3.MainActivity.class);
                  startActivity(intent);
               }
            }
         }
         @Override 
         public boolean onOptionsItemSelected(MenuItem item) { 
            super.onOptionsItemSelected(item); 
  
            switch(item.getItemId()){ 
               case R.id.item1: 
                  Bundle dataBundle = new Bundle();
                  dataBundle.putInt("id", 0);
                  Intent intent = new Intent(getApplicationContext(),com.example.database3.DisplayContact.class);
                  intent.putExtras(dataBundle);
                  startActivity(intent);
  
                    Bundle extras = getIntent().getExtras();
  
                       int Value = extras.getInt("id");
                       if(Value&gt;0){
                          if(mydb.updateContact(id_To_Update,name.getText().toString(), 
                              note.getText().toString(),
                              txtView.getText().toString())){
                                  Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();  
                           }      
                          else{
                             Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();   
                          }
                       }
                       else{
                          if(mydb.insertContact(name.getText().toString(),
                              note.getText().toString(),
                              txtView.getText().toString())){
                                  Toast.makeText(getApplicationContext(), "Added", Toast.LENGTH_SHORT).show();    
                          }       
                      else{
                         Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();  
                      }
                       }
              finish();
              return true; 
              default: 
              return super.onOptionsItemSelected(item); 
             } 
         } 
  
         @Override
         public boolean onKeyDown(int keycode, KeyEvent event) {
                if (keycode == KeyEvent.KEYCODE_BACK) {
                   Intent a = new Intent(this,MainActivity.class);
                   a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                   startActivity(a);
                   return true;
                }
                return super.onKeyDown(keycode, event);
             }
      }
  </code></pre>
  
  <p><strong>DBHelper.java</strong></p>
  
  <pre><code>import java.util.ArrayList;
  //import java.util.HashMap;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.DatabaseUtils;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  
  public class DBHelper extends SQLiteOpenHelper {
  
  
         public static final String DATABASE_NAME = "MyDBName.db";
         public static final String CONTACTS_TABLE_NAME = "contacts";
         public static final String CONTACTS_COLUMN_ID = "id";
         public static final String CONTACTS_COLUMN_NAME = "name";
         public static final String CONTACTS_COLUMN_NOTE = "note";
         public static final String CONTACTS_COLUMN_DATE = "date";
  
  //     private HashMap hp;
  
         public DBHelper(Context context) {
            super(context, DATABASE_NAME , null, 1);
         }
  
         @Override
         public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(
                    "create table contacts " +
                    "(id integer primary key," +
                    " name text," +
                    " note text," +
                    " date text)"
                    );
         }
  
         @Override
         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS contacts");
            onCreate(db);
         }
  
         public boolean insertContact  (String name, String note, String date) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
  
            contentValues.put("name", name);
            contentValues.put("date", date);  
            contentValues.put("note", note);
  
            db.insert("contacts", null, contentValues);
            return true;
         }
         public Cursor getData(int id){
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor res =  db.rawQuery( "select * from contacts where id="+id+"", null );
            return res;
         }
  
         public int numberOfRows(){
            SQLiteDatabase db = this.getReadableDatabase();
            int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
            return numRows;
         }
  
         public boolean updateContact (Integer id, String name, String note, String date){
  
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("name", name);
            contentValues.put("note", note);
            contentValues.put("date", date);
            db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
            return true;
         }
  
         public Integer deleteContact (Integer id){
            SQLiteDatabase db = this.getWritableDatabase();
            return db.delete("contacts", 
            "id = ? ", 
            new String[] { Integer.toString(id) });
         }
  
         public ArrayList &lt;String&gt; getAllCotacts(){
  
            ArrayList&lt;String&gt; array_list = new ArrayList&lt;String&gt;();
            //hp = new HashMap();
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor res =  db.rawQuery( "select * from contacts", null );
            res.moveToFirst();
            while(res.isAfterLast() == false){
            array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_DATE)) + "    " + res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
            res.moveToNext();
            }
         return array_list;
         }
      }
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>import java.util.ArrayList;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.KeyEvent;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  
  public class MainActivity extends Activity {
         public final static String EXTRA_MESSAGE = "com.example.database3.MESSAGE";
  
         private ListView obj;    
         DBHelper mydb;
  
         @Override
         protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
  
            mydb = new DBHelper(this);
            ArrayList &lt;String&gt; array_list = mydb.getAllCotacts();
  
            ArrayAdapter &lt;String&gt; arrayAdapter = new  ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1, array_list);
  
            //adding it to the list view.
            obj = (ListView)findViewById(R.id.listView1);
            obj.setAdapter(arrayAdapter);
  
            obj.setOnItemClickListener(new OnItemClickListener(){
  
           @Override
           public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
           long arg3) {
               // TODO Auto-generated method stub
               int id_To_Search = arg2 + 1;
               Bundle dataBundle = new Bundle();
               dataBundle.putInt("id", id_To_Search);
               Intent intent = new Intent(getApplicationContext(),com.example.database3.DisplayContact.class);
               intent.putExtras(dataBundle);
               startActivity(intent);
           }
           });
         }
         @Override
         public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.mainmenu, menu);
            return true;
            }
         @Override 
         public boolean onOptionsItemSelected(MenuItem item){ 
  
            super.onOptionsItemSelected(item); 
            switch(item.getItemId()) 
            { 
               case R.id.item1: 
                  Bundle dataBundle = new Bundle();
                  dataBundle.putInt("id", 0);
                  Intent intent = new Intent(getApplicationContext(),com.example.database3.DisplayContact.class);
                  intent.putExtras(dataBundle);
                  startActivity(intent);
                  return true; 
               default: 
                  return super.onOptionsItemSelected(item); 
  
             } 
  
         } 
         public boolean onKeyDown(int keycode, KeyEvent event) {
            if (keycode == KeyEvent.KEYCODE_BACK) {
               moveTaskToBack(true);
            }
            return super.onKeyDown(keycode, event);
         }
  
      }
  </code></pre>