Good afternoon, in my application has a database that shows the results of the game, I put a button that the function of it will delete all the data from the bank ... how do I do this part? in blogs I did not quite understand ...
My get and set:
package Base;
public class Esporte {
private int id;
private String nomeTimeUm;
private String nomeTimeDois;
private int valorUm;
private int valorDois;
public Esporte(){}
public Esporte(int id, String nomeTimeUm, String nomeTimeDois, int valorUm, int valorDois){
this.id = id;
this.nomeTimeUm = nomeTimeUm;
this.nomeTimeDois = nomeTimeDois;
this.valorUm = valorUm;
this.valorDois = valorDois;
}
public int getId(){return id;}
public void setId(int id){this.id = id;}
public String getNomeTimeUm(){return nomeTimeUm;}
public void setNomeTimeUm(String nomeTimeUm){this.nomeTimeUm = nomeTimeUm;}
public String getNomeTimeDois(){return nomeTimeDois;}
public void setNomeTimeDois(String nomeTimeDois){this.nomeTimeDois = nomeTimeDois;}
public int getValorUm(){return valorUm;}
public void setValorUm(int valorUm){this.valorUm = valorUm;}
public int getValorDois(){return valorDois;}
public void setValorDois(int valorDois){this.valorDois = valorDois;}
@Override
public String toString(){
return nomeTimeUm+ " X " +nomeTimeDois+
" = " +valorUm+ " X " +valorDois;
}
}
My DbHelper:
package Base;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DbHelper extends SQLiteOpenHelper {
private static final String NAME_BASE = "Resultados";
private static final int VERSION_BASE = 1;
public DbHelper(Context context) {
super(context, NAME_BASE, null, VERSION_BASE);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sqlCreateTableResultado = "CREATE TABLE resultado("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "TimeCasa TEXT,"
+ "TimeFora TEXT,"
+ "GolsCasa INTEGER,"
+ "GolsFora INTEGER"+")";
db.execSQL(sqlCreateTableResultado);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sqlDropTableResultado = "DROP TABLE resultado";
db.execSQL(sqlDropTableResultado);
onCreate(db);
}
public void insertResultado(Esporte resultado){
SQLiteDatabase db = getWritableDatabase();
ContentValues valores = new ContentValues();
valores.put("TimeCasa", resultado.getNomeTimeUm());
valores.put("TimeFora", resultado.getNomeTimeDois());
valores.put("GolsCasa", resultado.getValorUm());
valores.put("GolsFora", resultado.getValorDois());
db.insert("resultado", null, valores);
db.close();
}
public List<Esporte> selectTodosResult(){
List<Esporte> listResult = new ArrayList<Esporte>();
SQLiteDatabase db = getReadableDatabase();
String sqlSelectTodosResult = "SELECT * FROM resultado";
Cursor c = db.rawQuery(sqlSelectTodosResult, null);
if (c.moveToFirst()){
do {
Esporte onde = new Esporte();
onde.setId(c.getInt(0));
onde.setNomeTimeUm(c.getString(1));
onde.setNomeTimeDois(c.getString(2));
onde.setValorUm(c.getInt(3));
onde.setValorDois(c.getInt(4));
listResult.add(onde);
}
while (c.moveToNext());
}
db.close();
return listResult;
}
}
Activity Class:
package com.allsport.miyonic.allsport;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
import Base.DbHelper;
import Base.Esporte;
public class ResultSimples extends AppCompatActivity {
private ListView lista;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_simples);
lista = (ListView) findViewById(R.id.ListaTimes);
}
@Override
public void onResume(){
super.onResume();
DbHelper dbhe = new DbHelper(this);
List<Esporte> listaResultPartida = dbhe.selectTodosResult();
ArrayAdapter<Esporte> adp = new ArrayAdapter<Esporte>(this, android.R.layout.simple_list_item_1, listaResultPartida);
lista.setAdapter(adp);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.allsport.miyonic.allsport.ResultadoSimples"
android:background="#003366">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/ListaTimes"
android:textColor="#fff"
android:layout_marginTop="90dp" />
<TextView
android:text="Resultado das Partidas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100px"
android:id="@+id/textView"
android:textColor="#fff"
android:textSize="29dp"
android:textStyle="normal|italic"
android:textAlignment="center" />
<Button
android:text="Deletar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btndeletar"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"/>
</RelativeLayout>
Thank you ...