目前我正在尝试创建一个电话簿应用程序,当单击删除按钮时,将删除屏幕上显示的SQLite数据库中的条目。但是,当我单击删除按钮时,该方法运行但数据保留在sql数据库表中,直到我重新启动我正在使用的模拟器。重新启动时,数据表已更新,我的“下一步”和“上一页”按钮不会获取以前删除的数据。
有谁知道这是为什么?我将提出一些相关的代码..
public void Deletedata (View view)
{
int dRow;
dRow = Integer.parseInt((cursor.getString(0)));
db.deleteRecord(dRow);
Toast.makeText(this, "Contact Deleted", Toast.LENGTH_LONG).show();
Nextdata(view);
}
public void DisplayRecord(Cursor c)
{
EditText nameTxt = (EditText)findViewById(R.id.Name);
EditText phoneTxt = (EditText)findViewById(R.id.Phone);
EditText emailTxt = (EditText)findViewById(R.id.Email);
if (c!= null)
{
nameTxt.setText(c.getString(1));
phoneTxt.setText(c.getString(2));
emailTxt.setText(c.getString(3));
}
}
public void Nextdata (View view)
{
if (cursor.moveToNext())
{
DisplayRecord(cursor);
}
else
{
Toast.makeText(this, "Last Entry in Phone Book", Toast.LENGTH_LONG).show();
Previousdata(view);
}
}
public void Previousdata (View view)
{
if (cursor.moveToPrevious())
{
DisplayRecord(cursor);
}
else
{
Toast.makeText(this, "First Entry in Phone Book", Toast.LENGTH_LONG).show();
Nextdata(view);
}
这段代码在我的DBAdaptor类中,
public boolean deleteRecord(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor getAllRecords()
{
Cursor gaRecords = db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
KEY_PHONENUMBER, KEY_EMAIL}, null, null, null, null, null);
gaRecords.moveToFirst();
return gaRecords;
}
答案 0 :(得分:1)
您需要刷新光标。我对您的代码的最佳猜测是:
public void Deletedata (View view)
{
int position = cursor.getPosition();
int dRow;
dRow = Integer.parseInt((cursor.getString(0)));
db.deleteRecord(dRow);
Toast.makeText(this, "Contact Deleted", Toast.LENGTH_LONG).show();
// Refresh the cursor
cursor.close();
cursor = db.getAllRecords();
cursor.moveToPosition(position);
Nextdata(view);
}