我有一个我创建的数据库和一个使用列表视图的列表类。我遇到的问题是,当我想从列表中选择一个项目时,它会崩溃并给我以下错误:11-03 19:57:16.064: E/AndroidRuntime(27491): FATAL EXCEPTION: main 11-03 19 :57:16.064: E/AndroidRuntime(27491): android.database.CursorIndexOutOfBoundsException: 请求索引 0,大小为 0
我已打印到日志以查看光标是否正在获取任何信息并且它返回零,所以我知道它不是。我尝试了多种形式的 where 子句并且都返回相同的错误。您可以提供的任何帮助将不胜感激。
下面是数据库代码:
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();
}
}
这是我访问列表上的选择单击的方式:
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();
}
});