SQlite删除功能不是删除条目

时间:2013-08-07 17:23:51

标签: java android

我无法在我的应用程序正在使用的数据库中删除单个项目。我知道该方法被调用,但我的列表中没有任何内容被删除。我没有得到任何令人难以追查的错误。援助会很棒。

public class MainActivity extends Activity{

//Global Variables
ListView lv;
Intent addM, viewM;
public DBAdapter movieDatabase;
String tempTitle, tempYear;
int request_Code = 1;
int request_code2 = 2;
SimpleCursorAdapter dataAdapter;
Cursor cursor;
Button addButton;
long testID;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    //creates the database
    movieDatabase = new DBAdapter(this);
    movieDatabase.open();
    //movieDatabase.deleteAllMovies();


    //creates the intents to start the sub activities
    addM = new Intent(this, AddMovie.class);
    viewM = new Intent(this, MovieView.class);


    }
        //handles the return of the activity addMovie
        public void onActivityResult(int requestCode, int resultCode,    
Intent data)
        {
            super.onActivityResult(requestCode, resultCode, data);

            if(resultCode == RESULT_OK)
            {
                switch(requestCode)
                {
                case 1:
                    dbAddMovie(data.getStringExtra("title"), 
data.getStringExtra("year"));
                    break;
                case 2:
                    testID = data.getLongExtra("rowid", -1);
                    dMovie(testID);
                    break;
                }
            }

        }
        //adds item to the movie list
        public void dbAddMovie(String mT, String mY)
        {
            movieDatabase.open();
            movieDatabase.insertMovie(mT, mY);
            Toast.makeText(this, "Movie: " + mT + " added to database",    
Toast.LENGTH_SHORT).show();
        }

        //deletes an entry into the database
        public void dMovie(long rowid)
        {
            //Toast.makeText(this, "Deleting: " + rowid,   
Toast.LENGTH_SHORT).show();
            movieDatabase.deleteMovie(rowid);
            movieDatabase.getAllMovies();
        }

        //displays the database as a list
        public void displayListView()
        {
            addButton = (Button) findViewById(R.id.add);
            addButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                    startActivityForResult(addM, 1);    
                }
            });

            cursor = movieDatabase.getAllMovies();

             //columns to use
            String[] columns = new String[]
                    {
                    movieDatabase.KEY_TITLE,
                    };

            //xml data to bind the data to
            int[] to = new int[]
                    {
                    R.id.column2,
                    };

            //adapter to display the database as a list
            dataAdapter = new SimpleCursorAdapter(this,    
R.layout.complexrow, cursor, columns, to, 0);

            //gets the List view resource
            lv = (ListView) findViewById(R.id.movielist);

            //sets the list view to use the adapter
            lv.setAdapter(dataAdapter);

            //handles the list click events
            lv.setOnItemClickListener(new    
AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View 
v, int position,
                        long id) {

                    Cursor cursor = (Cursor) 
parent.getItemAtPosition(position);
                    Bundle mDet = new Bundle();
                    mDet.putString("title", 
cursor.getString(cursor.getColumnIndex(movieDatabase.KEY_TITLE)));
                    mDet.putString("year", 
cursor.getString(cursor.getColumnIndex(movieDatabase.KEY_YEAR)));
                    mDet.putInt("rId", position);
                    viewM.putExtras(mDet);
                    startActivityForResult(viewM, 2);
                }
            });
            //dataAdapter.notifyDataSetChanged();

        }   


        public void onResume()
        {
            super.onResume();
            displayListView();
        }
}

和我的相应dbadapter类

public class DBAdapter {

public static final String KEY_ROWID = "_id";
public static final String KEY_TITLE = "title";
public static final String KEY_YEAR = "year";
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "MovieListDB";
private static final String DATABASE_TABLE = "MoviesTable";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE = "create table MoviesTable (_id 
integer primary key autoincrement, " +
"title text not null, year not null);";

private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx)
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try{
            db.execSQL(DATABASE_CREATE);
        } catch (SQLException e)
        {
            e.printStackTrace();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "   
 + newVersion +
                "which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS MoviesTable");
        onCreate(db);

    }

}

public DBAdapter open() throws SQLException
{
    db = DBHelper.getWritableDatabase();
    return this;
}


public void close()
{
    DBHelper.close();
}

public long insertMovie(String title, String year)
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_TITLE, title);
    initialValues.put(KEY_YEAR, year);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

public boolean deleteMovie(long rowID)
{
    return db.delete(DATABASE_TABLE, KEY_ROWID + "='" + rowID+"'", null ) >-1;
}

public Cursor getAllMovies()
{
    return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, 
KEY_YEAR}, null, null, null, null, null);
}

public Cursor getMovie(long rowID) throws SQLException
{
    Cursor mCursor = 
            db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, 
KEY_TITLE, KEY_YEAR}, KEY_ROWID + "=" + rowID, null, null, null, null, null);
    if(mCursor != null)
    {
        mCursor.moveToFirst();
    }
    return mCursor;
}

public boolean updateContact(long rowID, String title, String year)
{
    ContentValues args = new ContentValues();
    args.put(KEY_TITLE, title);
    args.put(KEY_YEAR, year);
    return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowID, null) > 0;
}


public void deleteAllMovies() {
     int doneDelete = 0;
     doneDelete = db.delete(DATABASE_TABLE, null, null);

}

}

2 个答案:

答案 0 :(得分:1)

  • 您使用列表视图中返回的position作为数据库中的行ID。这不一定与数据库中的自动增量"_id"匹配。 position就是列表中的位置。

  • 您可能需要考虑使用movieDatabase.KEY_ROWID作为意图的关键。现在我看到了"rowid""rId""_id"KEY_ROWID的混合。在引用相同的东西时,只需在任何地方使用相同的密钥即可简化。

  • 看起来您不断向viewM意图添加捆绑包。真的吗?如果这不是你的意图,你应该为每次点击创建一个新意图,或者先删除以前的包。

答案 1 :(得分:0)

我假设KEY_ROWID实际上是列的名称?请尝试以下方法:

public boolean deleteMovie(long rowID)
{
    return db.delete(DATABASE_TABLE, KEY_ROWID + "=?", new String[] { String.valueOf(rowID) }) >-1;
}
相关问题