このメソッドはデータベース クラスにあります
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);
}
}
}