0

目前我正在尝试创建一个电话簿应用程序,当单击删除按钮时,将删除屏幕上显示的 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;

    }
4

1 回答 1

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);             
}
于 2013-03-10T17:01:42.430 回答