Files
DEV/DEV4.5/exo4_controle

72 lines
2.1 KiB
Plaintext

package com.example.partition;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SponsorActivity extends AppCompatActivity {
private TextView adTextView;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sponsor);
adTextView = findViewById(R.id.adTextView);
db = new DBHelper(this).getReadableDatabase();
updateAdText();
}
private void updateAdText() {
Cursor cursor = db.rawQuery("SELECT text FROM ad_table LIMIT 1", null);
if (cursor.moveToFirst()) {
adTextView.setText(cursor.getString(0));
}
cursor.close();
}
}
Fichier DBHelper.java
package com.example.partition;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "ads.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE ad_table (text TEXT)");
db.execSQL("INSERT INTO ad_table VALUES ('Publicité initiale')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS ad_table");
onCreate(db);
}
}
Fichier activity_sponsor.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp">
<TextView
android:id="@+id/adTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Publicité en cours de chargement..."
android:textSize="18sp"/>
</RelativeLayout>