游标索引超出绑定错误

时间:2017-01-20 17:41:02

标签: android indexoutofboundsexception android-cursoradapter

我正在试验数据库,在这里我没有使用辅助类,但在活动中添加了db函数。尝试从获取的行中选择一列时,我有一个索引超出范围的错误。

特别是

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是指向错误的行。无法弄清楚为什么。虽然自动增量变量在输入的文件夹旁边的相应文本视图中正确显示,但我无法获取它。

1 个答案:

答案 0 :(得分:0)

  

android.database.CursorIndexOutOfBoundsException

此异常表示您尝试使用无效索引访问游标。获取光标对象后,请确保调用moveToFirst()方法。这将返回一个布尔值,表示您的光标是否有任何数据,如果它有任何数据,则将光标设置为第一个元素。

替换它:

bundle.putInt("int",f.getInt(f.getColumnIndexOrThrow("_id")));

有了这个:

if (f != null && f.moveToFirst()) {
       bundle.putInt("int",f.getInt(f.getColumnIndexOrThrow("_id")));
}

如需进一步帮助,请参阅以下答案:android.database.CursorIndexOutOfBoundsException