0

Kindly note that I am trying to retrieve values from table in SQLite DB in my android app, on a button click event and present it through a ListView.I am able to insert records into the table on same event.Although it throws the following error:

`05-15 11:54:16.721: W/System.err(1834): android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
05-15 11:54:16.721: W/System.err(1834):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:434)
05-15 11:54:16.721: W/System.err(1834):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
05-15 11:54:16.721: W/System.err(1834):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
`

Follwing is the chunk of code I am using to Insert,Retrieve and Display the records from table in ListView:

        save.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
              event=spinner.getSelectedItem().toString();
              message=ed1.getText().toString();
              MainActivity.myDataBase.openDatabase("/data/data/org.mithil.appname/databases/tablename", null,MainActivity.myDataBase.OPEN_READWRITE);
              MainActivity.myDataBase.execSQL("INSERT INTO " +
                      MainActivity.tablename +
                        " Values ('"+ finalnumber +"','"+ name +"','"+ message +"','"+ event +"','"+ ldate +"');");
              Cursor cu = MainActivity.myDataBase.rawQuery("SELECT Name FROM " + MainActivity.tablename + " WHERE Date =" + ldate +";" , null);
              cu.moveToFirst();
              Log.d("first rec", cu.toString());
              while(cu!=null)
              {
                  try{
                  lstitem.add(cu.getString(0));
                  cu.moveToNext();
                  }
                  catch(Exception e)
                  {
                      e.printStackTrace();
                  }
              }

All this is happening in a button click.The insertions are getting reflected in table but the 'Select' is not.I reckon something is wrong with moving the cursor.The table consist of five fields off which I want to retrieve only one ie. 'Name' field and add the name into the ListView.Kindly correct me where I am going wrong.Thanks a lot.

3
  • Hi thanks for suggestion.there is no hasNext() method for Cursor class.But there is a method called 'isAfterLast()' which points to the position after last row.I will try using that.Will let you know. Commented May 15, 2013 at 6:49
  • Had that comment there for a second before I i removed it. was thinking about python. :P Commented May 15, 2013 at 6:51
  • Thank you @Gjordis,your solution helped.Much appreciated.May the force be with you. :) Commented May 15, 2013 at 7:11

3 Answers 3

2

You should get the values from the cursor like this

if(cu.moveToFirst())
{
do
{

}while(cu.moveToNext())
}
Sign up to request clarification or add additional context in comments.

Comments

1
while(cu !isAfterLast()){...

Shouldn't go out of bounds then. It can be not-null and still go out of bounds.

Comments

0

try this just get the cursor count and set it as a condition in for loop and iterate through it.

 for(int i=0;i<cr.getCount;i++)
{
//do your stuff here;
cr.moveToNext();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.