我刚刚开始学习SQlite,并在tutorialspoint上尝试了该示例。但是,从数据库中删除行时,下一个列表元素将获取其前面的元素的值,而我删除的那个元素之后的list元素将获取空值,并最终使游标索引超出范围。
public class DisplayEvent extends AppCompatActivity {
int from_Where_I_Am_Coming = 0;
private DBHelper mydb ;
TextView name ;
TextView descr;
int id_To_Update = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_event);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
name = (TextView) findViewById(R.id.editTextName);
descr = (TextView) findViewById(R.id.editTextDescr);
mydb = new DBHelper(this);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
int Value = extras.getInt("id");
if(Value>0){
Cursor rs = mydb.getData(Value);
id_To_Update = Value;
rs.moveToFirst();
String nam =
rs.getString(rs.getColumnIndex(DBHelper.EVENTS_COLUMN_NAME));
String des =
rs.getString(rs.getColumnIndex(DBHelper.EVENTS_COLUMN_DESCR));
if (!rs.isClosed()) {
rs.close();
}
Button b = (Button) findViewById(R.id.button11);
b.setVisibility(View.INVISIBLE);
name.setText((CharSequence) nam);
name.setFocusable(false);
name.setClickable(false);
descr.setText((CharSequence) des);
descr.setFocusable(false);
descr.setClickable(false);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
Bundle extras = getIntent().getExtras();
if(extras !=null) {
int Value = extras.getInt("id");
if(Value>0){
getMenuInflater().inflate(R.menu.display_contact, menu);
} else{
getMenuInflater().inflate(R.menu.menu_main, menu);
}
}
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case R.id.Edit_event:
Button b = (Button)findViewById(R.id.button11);
b.setVisibility(View.VISIBLE);
name.setEnabled(true);
name.setFocusableInTouchMode(true);
name.setClickable(true);
descr.setEnabled(true);
descr.setFocusableInTouchMode(true);
descr.setClickable(true);
return true;
case R.id.Delete_event:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteEvent)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteEvent(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),Calendar.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
这是我的DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
public Integer deleteEvent (Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("events",
"id = ? ",
new String[] { Integer.toString(id) });
}