1

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

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);

}

}
4

2 回答 2

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

  • 您可能想考虑使用movieDatabase.KEY_ROWID作为您的意图的关键。现在我看到了"rowid", "rId","_id"和的混合KEY_ROWID。当引用相同的东西时,在任何地方都使用相同的键会简化事情。

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

于 2013-08-07T17:51:24.560 回答
0

我假设KEY_ROWID实际上是列的名称?尝试以下操作:

public boolean deleteMovie(long rowID)
{
    return db.delete(DATABASE_TABLE, KEY_ROWID + "=?", new String[] { String.valueOf(rowID) }) >-1;
}
于 2013-08-07T17:49:59.990 回答