2013-08-07 68 views
1

Im我的應用程序正在使用的數據庫中刪除單個項目以刪除問題。我知道該方法被調用,但是我的列表中沒有任何內容被刪除。我沒有得到任何錯誤,這使得追查很難。援助會很棒。SQlite刪除功能不會刪除條目

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

和我coresponding將對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); 

} 

} 
+0

你說你知道它被調用...當被調用時'testID' /'rowid'的值是什麼? – Geobits

+0

它取決於我點擊的元素。我在dMovie中有一個吐司項目,它會根據我點擊的內容返回值。如果我將列表中的第一項打到最後一項,則範圍爲0 – user519670

+0

能否從DBAdapter類發佈更多代碼?我假設它擴展了SQLiteOpenHelper?最重要的是,你調用getWritableDatabase(),而不是getReadableDatabase()? – Aaron

回答

1
  • 您使用的是position從你的列表視圖在數據庫中的行ID返回。這不一定與您的數據庫中的自動增量"_id"相匹配。 position就是它在列表中的位置。

  • 您可能想要考慮使用movieDatabase.KEY_ROWID作爲您的意圖的關鍵。現在我看到"rowid","rId","_id"KEY_ROWID的混合。當談到同樣的事情時,只要在任何地方使用相同的密鑰就可以簡化它。

  • 它看起來像你不斷添加到viewM意圖捆綁。真的嗎?如果這不是您的意圖,您應該爲每次點擊創建一個新的意圖,或者先刪除先前的包。

+1

我剛剛注意到這樣的事情。我卸載了應用程序並重新安裝了它。我點擊了一個項目,即我的列表中的第三個項目。它應該是rowID 3,我點擊刪除,它刪除rowID 0的項目,現在不會再做了,所以我認爲這起到了一部分。 – user519670

+1

+1,很好的建議。第一個特別重要。 onItemClick甚至給你一個ID,並且由於你使用的是SimpleCursorAdapter,它應該給你從數據庫中獲得相應的ID。所以用'mDet.putLong(「rId」,id);'替換'mDet.putInt(「rId」,position);''。 – Aaron

0

我假設KEY_ROWID實際上是列的名字?請嘗試以下操作:

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