Reputation: 7
I have a database that I have created and a list class that uses a list view. I am running into the issue that when I want to select an item from the list it crasshes and gives me the error of: 11-03 19:57:16.064: E/AndroidRuntime(27491): FATAL EXCEPTION: main 11-03 19:57:16.064: E/AndroidRuntime(27491): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
I have printed to the log to see whether the cursor is getting any information and it returns a zero so I know that it is not. I have tried many forms of a where clause and all return the same error. Any help that you can give will be greatly appreciated.
Here is the database code:
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseWish extends SQLiteOpenHelper
{
public static final int VERSION = 1;
public static final String TABLE_NAME = "WishList";
public static final String DBNAME = "wishList.sqlite";
public static final String ID = "id";
public static final String BOOK = "book";
static SQLiteDatabase db1;
public DataBaseWish(Context context)
{
super(context, DBNAME, null, VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db1)
{
// TODO Auto-generated method stub
createDatabase(db1);
}
private void createDatabase(SQLiteDatabase db1)
{
db1.execSQL("create table " + TABLE_NAME + "(" + ID + " integer primary key autoincrement not null, " + BOOK + " text " + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db1, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db1.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db1);
}
public void Insert(String book)
{
ContentValues values = new ContentValues();
values.put(BOOK, book);
db1.insert(TABLE_NAME, null, values);
}
public void deleteItem(String book)
{
db1.delete(TABLE_NAME, ID, new String[] {book});
}
public ArrayList<String> get()
{
String[] column = new String[] {BOOK};
Cursor c = db1.query(TABLE_NAME, column, null, null, null, null, null);
ArrayList<String> result = new ArrayList<String>();
c.moveToFirst();
while(!c.isAfterLast())
{
result.add(c.getString(0));
c.moveToNext();
}
return result;
}
public String getBook(Cursor c)
{
return(c.getString(0));
}
public Cursor getID(String book)
{
String[] args = {book};
String[] column = new String[] {BOOK};
Log.w("Print",args[0]);
return(db1.query(TABLE_NAME, column, "id='0'", null, null, null, null));
//return(db1.rawQuery("SELECT id, book FROM WishList WHERE id="+ book, null));
}
public void close()
{
db1.close();
}
public void open()
{
db1 = this.getWritableDatabase();
}
}
and here is how I am accessing the select click on the list:
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> adapter, View view, int position, long id)
{
dbo.open();
dbID = String.valueOf(id);
Cursor c = dbo.getID(dbID);
Log.w("whishlist", "Cursor size is:"+c.getCount());
c.moveToFirst();
item.setText(dbo.getBook(c));
dbo.close();
}
});
Upvotes: 0
Views: 519
Reputation: 4112
The ID column starts by 1, not 0, but in your getID() method you hardcoded checking for id=0, that's why the returned cursor is empty. By the way, you can check whether the cursor is empty by looking at the value returned by Cursor's moveToFirst() method. If it is false, the cursor is empty. The commented line works, but you must remove the "id" column in the select query, otherwise the getBook method will fail as it will access the "id" column instead of "book".
Upvotes: 1