我在游标查询中遇到问题。当我单击listview编辑/删除数据时,错误显示。
LogCat错误显示如下:
12-07 02:40:37.721: E/AndroidRuntime(1482): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myrecordapps/com.example.myrecordapps.EditCourse}: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
我的编码如下所示。
super.onCreate(savedInstanceState);
setContentView(R.layout.subject);
nameList = (ListView) findViewById(R.id.list);
registerBtn = (Button) findViewById(R.id.addsubject);
adapter_ob = new DBHandlerCourse(this);
String[] from = { DBHelperCourse.KEY_COURSE_NAME, DBHelperCourse.KEY_COURSE_CODE, DBHelperCourse.KEY_START_TIME};
int[] to = { R.id.coursename, R.id.coursecode, R.id.starttime };
cursor = adapter_ob.queryName();
@SuppressWarnings("deprecation")
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,R.layout.row, cursor, from, to);
nameList.setAdapter(cursorAdapter);
nameList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Bundle passdata = new Bundle();
Cursor listCursor = (Cursor) arg0.getItemAtPosition(arg2);
int nameId = listCursor.getInt(listCursor.getColumnIndex(DBHelperCourse.KEY_ID));
Toast.makeText(getApplicationContext(),Integer.toString(nameId), 500).show();
passdata.putInt("keyid", nameId);
Intent passIntent = new Intent(Subject.this,EditCourse.class);
passIntent.putExtras(passdata);
startActivity(passIntent);
}
});
registerBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent registerIntent = new Intent(Subject.this,
AddCourse.class);
startActivity(registerIntent);
}
});
}
@SuppressWarnings("deprecation")
@Override
public void onResume() {
super.onResume();
cursor.requery();
}
public DBHandlerCourse(Context c) {
context = c;
}
public DBHandlerCourse opnToRead() {
openHelper_ob = new DBHelperCourse(context,
DBHelperCourse.DATABASE_NAME, null, DBHelperCourse.VERSION);
database_ob = openHelper_ob.getReadableDatabase();
return this;
}
public DBHandlerCourse opnToWrite() {
openHelper_ob = new DBHelperCourse(context,
DBHelperCourse.DATABASE_NAME, null, DBHelperCourse.VERSION);
database_ob = openHelper_ob.getWritableDatabase();
return this;
}
public void Close() {
database_ob.close();
}
public long insertDetails(String courseName, String courseCode, String lecturer, String creditHour, String startTime) {
ContentValues contentValues = new ContentValues();
contentValues.put(DBHelperCourse.KEY_COURSE_NAME, courseName);
contentValues.put(DBHelperCourse.KEY_COURSE_CODE, courseCode);
contentValues.put(DBHelperCourse.KEY_LECTURER, lecturer);
contentValues.put(DBHelperCourse.KEY_CREDIT_HOUR, creditHour);
contentValues.put(DBHelperCourse.KEY_START_TIME, startTime);
opnToWrite();
long val = database_ob.insert(DBHelperCourse.TABLE_NAME, null,
contentValues);
Close();
return val;
}
public Cursor queryName() {
String[] cols = { DBHelperCourse.KEY_ID, DBHelperCourse.KEY_COURSE_NAME,
DBHelperCourse.KEY_COURSE_CODE, DBHelperCourse.KEY_START_TIME};
opnToWrite();
Cursor c = database_ob.query(DBHelperCourse.TABLE_NAME, cols, null,
null, null, null, null);
return c;
}
public Cursor queryAll(int nameId) {
String[] cols = { DBHelperCourse.KEY_COURSE_NAME, DBHelperCourse.KEY_COURSE_CODE, DBHelperCourse.KEY_START_TIME };
opnToWrite();
Cursor c = database_ob.query(DBHelperCourse.TABLE_NAME, cols,
DBHelperCourse.KEY_ID + "=" + nameId, null, null, null, null);
return c;
}
public long updateldetail(int rowId, String courseName, String courseCode, String lecturer, String creditHour, String startTime) {
ContentValues contentValues = new ContentValues();
contentValues.put(DBHelperCourse.KEY_COURSE_NAME, courseName);
contentValues.put(DBHelperCourse.KEY_COURSE_CODE, courseCode);
contentValues.put(DBHelperCourse.KEY_LECTURER, lecturer);
contentValues.put(DBHelperCourse.KEY_CREDIT_HOUR, creditHour);
contentValues.put(DBHelperCourse.KEY_START_TIME, startTime);
opnToWrite();
long val = database_ob.update(DBHelperCourse.TABLE_NAME, contentValues,
DBHelperCourse.KEY_ID + "=" + rowId, null);
Close();
return val;
}
public int deletOneRecord(int rowId) {
// TODO Auto-generated method stub
opnToWrite();
int val = database_ob.delete(DBHelperCourse.TABLE_NAME,
DBHelperCourse.KEY_ID + "=" + rowId, null);
Close();
return val;
}
DBHandlerCourse regadapter;
DBHelperCourse openHelper;
int rowId;
Cursor c;
String courseNameValue, courseCodeValue, lecturerValue, creditHourValue, startTimeValue;
EditText courseName, courseCode, lecturer, creditHour, startTime;
Button editSubmit, btnDelete;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_edit_course);
courseName = (EditText) findViewById(R.id.et_course_name);
courseCode = (EditText) findViewById(R.id.et_course_code);
lecturer = (EditText) findViewById(R.id.et_lecturer);
creditHour = (EditText) findViewById(R.id.et_credit_hour);
startTime = (EditText) findViewById(R.id.et_start_time);
editSubmit = (Button) findViewById(R.id.btn_update);
btnDelete = (Button) findViewById(R.id.btn_delete);
Bundle showData = getIntent().getExtras();
rowId = showData.getInt("keyid");
// Toast.makeText(getApplicationContext(), Integer.toString(rowId),
// 500).show();
regadapter = new DBHandlerCourse(this);
c = regadapter.queryAll(rowId);
if (c.moveToFirst()) {
do {
courseName.setText(c.getString(1));
courseCode.setText(c.getString(2));
lecturer.setText(c.getString(3));
creditHour.setText(c.getString(4));
startTime.setText(c.getString(5));
} while (c.moveToNext());
}
editSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
regadapter.updateldetail(
rowId, courseName.getText().toString(),
courseCode.getText().toString(),
lecturer.getText().toString(),
creditHour.getText().toString(),
startTime.getText().toString());
finish();
}
});
btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
regadapter.deletOneRecord(rowId);
finish();
}
});
}
答案 0 :(得分:0)
问题在于:
if (c.moveToFirst()) {
do {
courseName.setText(c.getString(1));
courseCode.setText(c.getString(2));
lecturer.setText(c.getString(3));
creditHour.setText(c.getString(4));
startTime.setText(c.getString(5));
} while (c.moveToNext());
您的查询只包含三列:
public Cursor queryAll(int nameId) {
String[] cols = { DBHelperCourse.KEY_COURSE_NAME, DBHelperCourse.KEY_COURSE_CODE, DBHelperCourse.KEY_START_TIME };
opnToWrite();
Cursor c = database_ob.query(DBHelperCourse.TABLE_NAME, cols,
DBHelperCourse.KEY_ID + "=" + nameId, null, null, null, null);
return c;
}
但是你使用的不仅仅是导致IllegalStateException
的因素。另请注意,游标索引以0开头。