0

此方法在我的数据库类中

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

但我仍然从我的列表视图中的先前条目中获取数据。我也试过

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

  }

}

4

2 回答 2

0

从您的班级发布一些代码,在您初始化适配器时DBAdapter可以快速替换。删除项目后,您还会刷新列表视图吗?如果没有,请调用您的 listview 适配器。getApplicationContext()thisnotifyDataSetChanged()

于 2013-04-17T17:35:30.870 回答
0

deletePlayers如果要清除所有玩家,请不带参数创建并调用它。

public boolean deletePlayers()
{
    return this.mDb.delete("Players", "1", null) > 0;
}
于 2013-04-17T19:20:57.607 回答