如何在我的应用程序开始时调用此方法清除数据库?

时间:2013-04-17 17:11:23

标签: android boolean public

此方法位于我的数据库类

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

我想在我的Launch Activity类中调用它

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

这就是我试过的

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


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

但我仍然从listview中的先前条目中获取数据。 我也试过

db.deletePlayers(1);

无济于事

完整适配器类

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

  }

}

2 个答案:

答案 0 :(得分:0)

发布DBAdapter课程中的一些代码,初始化适配器时,快速搜索可以将getApplicationContext()替换为this。删除项目后,还会刷新列表视图吗?如果没有,请通过调用listview适配器上的notifyDataSetChanged()来执行此操作。

答案 1 :(得分:0)

创建没有参数的deletePlayers,如果要清除所有玩家,请调用此方法。

public boolean deletePlayers()
{
    return this.mDb.delete("Players", "1", null) > 0;
}