title: Cursor index out of bounds Exception, Android database
body: <p>I am doing an android app. I am trying to get the rows of the database a view them in a text view. At the begining I want to take the first row of the database. Then I use the gyroscope in order to slide the quotes in the screen. But I get this error. Can anyone help me please? Thanks a lot.</p>
  
  <p>This is SlideShow class</p>
  
  <pre><code>package com.example.prova1;
  
  import java.util.HashMap;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.hardware.Sensor;
  import android.hardware.SensorEvent;
  import android.hardware.SensorEventListener;
  import android.hardware.SensorManager;
  import android.os.Bundle;
  import android.widget.TextView;
  
  
  
  public class SlideQuote extends Activity implements SensorEventListener
  {
   //a TextView
   private TextView quote;
   private TextView author;
   //the Sensor Manager
   private SensorManager sManager;
   float x;
   int id;
   int total;
   String s1,s2;
    Database quotedatabase = new Database(this);
  
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.slide_quote);
  
          //get the TextView from the layout file
          quote = (TextView) findViewById(R.id.textView3);
          author = (TextView) findViewById(R.id.textView4);
  id=1;
          //get a hook to the sensor service
  
          sManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
       if(sManager.getSensorList(Sensor.TYPE_GYROSCOPE).size()!=0){
        Sensor s =sManager.getSensorList(Sensor.TYPE_GYROSCOPE).get(o);
        sManager.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL);
        quotedatabase.getQuote(id, s1, s2);
        quote.setText(s1);
        author.setText(s2);
      }
      }
      //when this Activity starts
      @Override
   protected void onResume()
   {
    super.onResume();
    /*register the sensor listener to listen to the gyroscope sensor, use the
    callbacks defined in this class, and gather the sensor information as quick
    as possible*/
    sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),SensorManager.SENSOR_DELAY_FASTEST);
   }
  
    //When this Activity isn't visible anymore
   @Override
   protected void onStop()
   {
    //unregister the sensor listener
    sManager.unregisterListener(this);
    super.onStop();
   }
  
   @Override
   public void onAccuracyChanged(Sensor arg0, int arg1)
   {
    //Do nothing.
   }
  
   @Override
   public void onSensorChanged(SensorEvent event)
   {
    //if sensor is unreliable, return void
    if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
    {
     return;
    }
    //else it will output the Roll, Pitch and Yawn values
    x=event.values[2];
    total=quotedatabase.getQuotesCount();
  
    if(x&gt;25){
     if(id==total)
     {id=1;
     }
     else{
  
     id++;
     }
        quotedatabase.getQuote(id, s1, s2);
        quote.setText(s1);
        author.setText(s2);
  
    }
    if(x&lt;-25){
     if(id==1)
     {id=total;}
     else{
     id--;}
  
        quotedatabase.getQuote(id, s1, s2);
        quote.setText(s1);
        author.setText(s2);
    }
    }
   @Override
   public void onBackPressed() {
    // TODO Auto-generated method stub
    sManager.unregisterListener(this);
    Intent backIntent = new Intent(getApplication(), Quote.class);
         finish();
         startActivity(backIntent);
  
   }
  
  
   }
  </code></pre>
  
  <p>And this is the database:</p>
  
  <pre><code>package com.example.prova1;
  
  /**This class is for the database that is used to store quotes*/
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  
  public class Database extends SQLiteOpenHelper {
  
   public Database(Context quoteContext){
  
   super(quoteContext, "quoteDB", null, 1);
  
   }
  
   @Override
   //Create database
   public void onCreate(SQLiteDatabase database) {
  
    String query = "CREATE TABLE quotelist ( quoteId INTEGER PRIMARY KEY, textQuote TEXT ,textAuthor TEXT)";
    String query1 = "INSERT INTO quotelist  VALUES ( '1', 'RAI', 'uku')";
  
  
      database.execSQL(query);
      database.execSQL(query1);
   }
  
   @Override
   public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
  
     String query = "DROP TABLE IF EXISTS quotelist";
  
     database.execSQL(query);
     onCreate(database);
    }
  
   //Insert quotes into database 
  public void insertItem(HashMap&lt;String, String&gt; queryValues){
  
    SQLiteDatabase database = this.getWritableDatabase();
  
    ContentValues values = new ContentValues();
  
    values.put("textQuote", queryValues.get("textQuote"));
    values.put("textAuthor", queryValues.get("textAuthor"));
  
    database.insert("quotelist", null, values);
  
    database.close();
  
   }
  
  //Update the quotes into the database
  
  public int updateItem(HashMap&lt;String, String&gt; queryValues) {
  
   SQLiteDatabase database = this.getWritableDatabase();
  
   ContentValues values = new ContentValues();
  
   values.put("textQuote", queryValues.get("textQuote"));
   values.put("textAuthor", queryValues.get("textAuthor"));
  
   return database.update("quotelist", values, "quoteId" + " = ?", new String[] {queryValues.get("quoteId") });
  
  }
  
  //Delete quotes into the database
  
  public void deleteItem(String id){
  
   SQLiteDatabase database = this.getWritableDatabase();
  
   String deleteQuery = "DELETE FROM quotelist WHERE quoteId='" + id + "'";
  
   database.execSQL(deleteQuery);
   database.close();
  
  
  }
  
  public ArrayList&lt;HashMap&lt;String, String&gt;&gt; getAllItems(){
  
   ArrayList&lt;HashMap&lt;String, String&gt;&gt; itemArrayList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
   String selectQuery = "SELECT * FROM quotelist";
  
   SQLiteDatabase database = this.getWritableDatabase();
  
   Cursor cursor = database.rawQuery(selectQuery, null);
  
   if(cursor.moveToFirst()){
  
    do{
  
     HashMap&lt;String, String&gt; itemMap = new HashMap&lt;String, String&gt;();
  
     itemMap.put("quoteId", cursor.getString(o));
     itemMap.put("textQuote", cursor.getString(1));
     itemMap.put("textAuthor", cursor.getString(2));
  
  
    itemArrayList.add(itemMap);
  
    } while(cursor.moveToNext());
  
   }
   database.close();
  
   return itemArrayList;
  
  }
  
  public HashMap&lt;String, String&gt; getItemInfo(String id){
  
   HashMap&lt;String, String&gt; itemMap = new HashMap&lt;String, String&gt;();
  
   SQLiteDatabase database = this.getReadableDatabase();
  
   String selectQuery = "SELECT * FROM quotelist WHERE quoteId='" + id + "'";
  
   Cursor cursor = database.rawQuery(selectQuery, null);
  
   if(cursor.moveToFirst()){
  
    do{
  
     itemMap.put("quoteId", cursor.getString(o));
     itemMap.put("textQuote", cursor.getString(1));
     itemMap.put("textAuthor", cursor.getString(2));
  
    } while(cursor.moveToNext());
  
   }
  
   return itemMap;
  
  }
  
  public void getQuote(int id, String s1,String s2) {
      SQLiteDatabase db = this.getReadableDatabase();
  
      Cursor cursor = db.query("quotelist", new String[] { "quoteId",
              "textQuote", "textAuthor" }, "quoteId" + "=?",
              new String[] { String.valueOf(id) }, null, null, null, null);
      if (cursor != null)
          cursor.moveToFirst();
  s1=cursor.getString(1);
  s2=cursor.getString(2);
  
  
  }
  public int getQuotesCount() {
      String countQuery = "SELECT  * FROM  quotelist" ;
      SQLiteDatabase db = this.getReadableDatabase();
      Cursor cursor = db.rawQuery(countQuery, null);
      cursor.close();
  
      // return count
      return cursor.getCount();
  }
  </code></pre>
  
  <p>The error is  atthis line :   s1=cursor.getString(1);  at DatabaseClass</p>
  
  <p>and at line: quotedatabase.getQuote(id, s1, s2); at ShowQuote class</p>