2
Good Afternoon, I made a delete method in my database and called it in my Activity, only my problem is that when I click on the button it doesn’t happen anything, and doesn’t present me any error in Logcat, someone would know to help me?
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;
}
public void delete(){
SQLiteDatabase db = getReadableDatabase();
String sqlSelectTodosResult = "DELETE * FROM resultado";
Cursor c = db.rawQuery(sqlSelectTodosResult, null);
}
}
My Activity:
package com.allsport.miyonic.allsport;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.List;
import Base.DbHelper;
import Base.Esporte;
import static android.os.FileObserver.DELETE;
public class ResultSimples extends AppCompatActivity {
private ListView lista;
private Button apagar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_simples);
lista = (ListView) findViewById(R.id.ListaTimes);
apagar = (Button) findViewById(R.id.btndeletar);
apagar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DbHelper dd = new DbHelper(ResultSimples.this);
dd.delete(); // É necessário passar o parâmetro
}
});
}
@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);
}
}
Thank you...
The error is on the line of
dd.delete();
– Nathan
Nathan the
dd.delete();
this without parameter, see that it asks for a number like longpublic void delete(long id)
.– Guilherme Nascimento
So @Guilhermenascimento could put dd.delete(1); that would be a long kind right? (in this case)
– Nathan
@Nathan depends, the ID has to come from somewhere, play a random number there will not make sense, the id has to exist in the bank
– Guilherme Nascimento