0

This Method is in my database class

 public boolean deletePlayers(long paramLong)
  {
    return this.mDb.delete("Players", "_id=" + paramLong, null) > 0;
  }

I want to call it in my Launch Activity class

public class LaunchActivity extends Activity {
 Button start;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homescreen);
        addListenerOnButton();
        PlayerDbAdapter db = new PlayerDbAdapter(getApplicationContext());
        db.open();
        db.deletePlayers(0);
        db.close();

    }
    public void addListenerOnButton() {

        final Context context = this;

        start = (Button)findViewById(R.id.startButton);

        start.setOnClickListener(new OnClickListener() {


        public void onClick(View view) {
    Intent intent = new Intent(context, LoginActivity.class);
                                    startActivity(intent); 
                                    finish();

This is what I tried

 PlayerDbAdapter db = new PlayerDbAdapter(getApplicationContext());
        db.open();


db.deletePlayers(0);
        db.close();

But I am still getting Data from previous entries in my listview. I also tried

db.deletePlayers(1);

To no avail

Full Adapter Class

public class PlayerDbAdapter {

private static final String DATABASE_TABLE = "Players";
private static final String DATABASE_CREATE = "CREATE TABLE "+DATABASE_TABLE+" (_id integer primary key autoincrement, Player_Name text not null, Player_Position text not null, Player_Number text not null, Team text not null);";
private static final int DATABASE_VERSION = 3;
  public static final String KEY_BODY = "Player_Name";
  public static final String KEY_ROWID = "_id";
  public static final String KEY_TITLE = "Player_Position";
  public static final String KEY_NUMBER = "Player_Number";
  public static final String KEY_TEAM = "Team";
private static final String TAG = "PlayerDbAdapter";
  private final Context mCtx;
  private SQLiteDatabase mDb;
  private DatabaseHelper mDbHelper;

  public PlayerDbAdapter(Context paramContext)
  {
    this.mCtx = paramContext;
  }

  public void close()
  {
    this.mDbHelper.close();
  }


  public long createPlayers(String playerName, String playerPosition, String playerNumber, String team)
  {
    ContentValues localContentValues = new ContentValues();
    localContentValues.put(KEY_BODY, playerName);
    localContentValues.put(KEY_TITLE, playerPosition);
    localContentValues.put(KEY_NUMBER, playerNumber);
    localContentValues.put(KEY_TEAM, team);
    try{
        return this.mDb.insert("Players", null, localContentValues);
    } catch (Exception e) {
        Log.e("Juma", e.getMessage());
    }

    return 0;
  }

  public boolean deletePlayers(long paramLong)
  {
    return this.mDb.delete("Players", "_id=" + paramLong, null) > 0;
  }

  public Cursor fetchAllPlayers()
  {
    return this.mDb.query("Players", new String[] { "_id", "Player_Name", "Player_Position","Player_Number","Team" }, null, null, null, null, null);
  }

  public Cursor fetchPlayer(long paramLong)
    throws SQLException
  {
    Cursor localCursor = this.mDb.query(true, "Players", new String[] { "_id", "Player_Name", "Player_Position", "Player_Number","Team" }, "_id=" + paramLong, null, null, null, null, null);
    if (localCursor != null)
      localCursor.moveToFirst();
    return localCursor;
  }

  public PlayerDbAdapter open()
    throws SQLException
  {
    this.mDbHelper = new DatabaseHelper(this.mCtx);
    this.mDb = this.mDbHelper.getWritableDatabase();
    return this;
  }

  public boolean updateNote(long paramLong, String paramString1, String paramString2,String paramString3,String paramString4)
  {
    ContentValues localContentValues = new ContentValues();
    localContentValues.put("Player_Name", paramString1);
    localContentValues.put("Player_Position", paramString2);
    localContentValues.put("Player_Number", paramString3);
    localContentValues.put("Team", paramString4);
    return this.mDb.update("players", localContentValues, "_id=" + paramLong, null) > 0;
  }

  private static class DatabaseHelper extends SQLiteOpenHelper
  {
    DatabaseHelper(Context paramContext)
    {
      super(paramContext, "Players", null, 1);
    }

    public void onCreate(SQLiteDatabase paramSQLiteDatabase)
    {
      paramSQLiteDatabase.execSQL("CREATE TABLE Players (_id integer primary key autoincrement, Player_Name text not null, Player_Position text not null,  Player_Number text not null, Team text not null);");
    }

    public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2)
    {
      Log.w("PlayerDbAdapter", "Upgrading database from version " + paramInt1 + " to " + paramInt2 + ", which will destroy all old data");
      paramSQLiteDatabase.execSQL("DROP TABLE IF EXISTS Players");
      onCreate(paramSQLiteDatabase);
    }

  }

}

b0w3rb0w3r
  • 927
  • 3
  • 11
  • 33

2 Answers2

0

Post some code from your DBAdapter class, a quick shot can be replacing getApplicationContext() by this when you initialize the adapter. And also do you refresh your listview once you delete the item? If not please do so by calling notifyDataSetChanged() on your listview adapter.

Munim
  • 2,626
  • 1
  • 19
  • 28
  • @Abdul tried notifyDataSetChanged (); ddint work. Updated OP with full Adapter class – b0w3rb0w3r Apr 17 '13 at 17:58
  • When you call `deletePlayers` what you get in return? True or False? – Munim Apr 17 '13 at 18:17
  • I breakpointed it and checked the variables this is what i found `mInnerTransactionIsSuccessful false` is this what you meant? – b0w3rb0w3r Apr 17 '13 at 19:06
  • No,your `deletePlayers` method returns a `boolean`;if it's successful it will return `true`;check like this: `if(deletePlayers(1)){Toast.makeText(this,"Must have deleted!",1000).show();} else{ //could not delete}` – Munim Apr 17 '13 at 20:31
0

Create deletePlayers with no param and call this if you want to clear all players.

public boolean deletePlayers()
{
    return this.mDb.delete("Players", "1", null) > 0;
}
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • If I get rid of the `(long paramLong)` it creates problem in one of my activity classes `public boolean onContextItemSelected(MenuItem item) { switch(item.getItemId()) { case DELETE_ID: AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); mDbHelper.deletePlayers(info.id); fillData(); return true; } return super.onContextItemSelected(item); }` I get the error `deletePlayers` is not applicable for the argument (long) – b0w3rb0w3r Apr 17 '13 at 19:42
  • So instead of change make another method as above. – Hoan Nguyen Apr 17 '13 at 19:52
  • Sorry didn't know about that feature. – b0w3rb0w3r Apr 17 '13 at 20:17