游标索引越界错误
Cursor index out of bound error
我正在试验数据库,这里我没有使用助手 class,而是在 activity 中添加了数据库函数。我在尝试 select 获取的行中的一列时遇到索引超出范围的错误。
具体来说
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
错误。
MainActivity
public class MainActivity extends AppCompatActivity {
SQLiteDatabase db;
private EditText foldernameEditText;
private ListView folderlistview;
private TextView mEmptyStateTextView;
ListViewCursorAdapter listViewCursorAdapter;
final String mfoldertable = "foldertable";
final String mtitletable = "titletable";
TextView transfer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = openOrCreateDatabase("mainDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS " + mfoldertable + "(_id integer primary key autoincrement, folder text collate NOCASE, UNIQUE(folder))");
db.execSQL("CREATE TABLE IF NOT EXISTS "+mtitletable+ "(_id integer primary key autoincrement,id1 integer, title text, FOREIGN KEY(id1) REFERENCES "+mfoldertable+"(_id))");
//db.execSQL("Drop table "+mfoldertable+"");
//db.execSQL("Drop table "+mtitletable+"");
Cursor c = db.rawQuery("SELECT * FROM " + mfoldertable + "", null);
listViewCursorAdapter = new ListViewCursorAdapter(this, c);
folderlistview = (ListView) findViewById(R.id.folderlistview);
mEmptyStateTextView = (TextView) findViewById(R.id.empty_view);
folderlistview.setAdapter(listViewCursorAdapter);
folderlistview.setEmptyView(mEmptyStateTextView);
foldernameEditText = (EditText) findViewById(R.id.foldername);
Button enterFolderButton = (Button) findViewById(R.id.enterFolderButton);
enterFolderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
db.execSQL("INSERT INTO "+mfoldertable+"(folder) VALUES('"+ foldernameEditText.getText().toString() +"')");
} catch (SQLiteConstraintException exception) {
Toast.makeText(getApplicationContext(), "This folder already exists", Toast.LENGTH_SHORT).show();
}
foldernameEditText.setText("");
Cursor d = db.rawQuery("SELECT * FROM " + mfoldertable + "", null);
listViewCursorAdapter.swapCursor(d);
}
});
folderlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this, TitleList.class);
Bundle bundle = new Bundle();
transfer = (TextView) view.findViewById(R.id.list_item_folder_text_view);
String stuff = transfer.getText().toString();
bundle.putString("hello", stuff);
Cursor f=db.rawQuery("SELECT * FROM "+mfoldertable+" where folder='"+stuff+'"",null);
//Log.e("helloerror",f.getString(f.getColumnIndexOrThrow("_id")));
bundle.putInt("int",f.getInt(f.getColumnIndexOrThrow("_id")));
//bundle.putInt("int",1);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
光标f是指向错误的那一行。不知道为什么。尽管自动增量变量正确显示在输入文件夹旁边的相应文本视图中,但我无法获取它。
android.database.CursorIndexOutOfBoundsException
此异常表示您正在尝试访问具有无效索引的游标。确保在获得光标对象后立即调用 moveToFirst()
方法。这将 return 一个布尔值,表示您的光标是否有任何数据,如果有任何数据,则将光标设置为第一个元素。
替换为:
bundle.putInt("int",f.getInt(f.getColumnIndexOrThrow("_id")));
有了这个:
if (f != null && f.moveToFirst()) {
bundle.putInt("int",f.getInt(f.getColumnIndexOrThrow("_id")));
}
如需进一步帮助,请参阅此答案:android.database.CursorIndexOutOfBoundsException
我正在试验数据库,这里我没有使用助手 class,而是在 activity 中添加了数据库函数。我在尝试 select 获取的行中的一列时遇到索引超出范围的错误。
具体来说
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
错误。
MainActivity
public class MainActivity extends AppCompatActivity {
SQLiteDatabase db;
private EditText foldernameEditText;
private ListView folderlistview;
private TextView mEmptyStateTextView;
ListViewCursorAdapter listViewCursorAdapter;
final String mfoldertable = "foldertable";
final String mtitletable = "titletable";
TextView transfer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = openOrCreateDatabase("mainDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS " + mfoldertable + "(_id integer primary key autoincrement, folder text collate NOCASE, UNIQUE(folder))");
db.execSQL("CREATE TABLE IF NOT EXISTS "+mtitletable+ "(_id integer primary key autoincrement,id1 integer, title text, FOREIGN KEY(id1) REFERENCES "+mfoldertable+"(_id))");
//db.execSQL("Drop table "+mfoldertable+"");
//db.execSQL("Drop table "+mtitletable+"");
Cursor c = db.rawQuery("SELECT * FROM " + mfoldertable + "", null);
listViewCursorAdapter = new ListViewCursorAdapter(this, c);
folderlistview = (ListView) findViewById(R.id.folderlistview);
mEmptyStateTextView = (TextView) findViewById(R.id.empty_view);
folderlistview.setAdapter(listViewCursorAdapter);
folderlistview.setEmptyView(mEmptyStateTextView);
foldernameEditText = (EditText) findViewById(R.id.foldername);
Button enterFolderButton = (Button) findViewById(R.id.enterFolderButton);
enterFolderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
db.execSQL("INSERT INTO "+mfoldertable+"(folder) VALUES('"+ foldernameEditText.getText().toString() +"')");
} catch (SQLiteConstraintException exception) {
Toast.makeText(getApplicationContext(), "This folder already exists", Toast.LENGTH_SHORT).show();
}
foldernameEditText.setText("");
Cursor d = db.rawQuery("SELECT * FROM " + mfoldertable + "", null);
listViewCursorAdapter.swapCursor(d);
}
});
folderlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this, TitleList.class);
Bundle bundle = new Bundle();
transfer = (TextView) view.findViewById(R.id.list_item_folder_text_view);
String stuff = transfer.getText().toString();
bundle.putString("hello", stuff);
Cursor f=db.rawQuery("SELECT * FROM "+mfoldertable+" where folder='"+stuff+'"",null);
//Log.e("helloerror",f.getString(f.getColumnIndexOrThrow("_id")));
bundle.putInt("int",f.getInt(f.getColumnIndexOrThrow("_id")));
//bundle.putInt("int",1);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
光标f是指向错误的那一行。不知道为什么。尽管自动增量变量正确显示在输入文件夹旁边的相应文本视图中,但我无法获取它。
android.database.CursorIndexOutOfBoundsException
此异常表示您正在尝试访问具有无效索引的游标。确保在获得光标对象后立即调用 moveToFirst()
方法。这将 return 一个布尔值,表示您的光标是否有任何数据,如果有任何数据,则将光标设置为第一个元素。
替换为:
bundle.putInt("int",f.getInt(f.getColumnIndexOrThrow("_id")));
有了这个:
if (f != null && f.moveToFirst()) {
bundle.putInt("int",f.getInt(f.getColumnIndexOrThrow("_id")));
}
如需进一步帮助,请参阅此答案:android.database.CursorIndexOutOfBoundsException