Ciao a tutti, come da oggetto (forse non propriamente chiaro) ho un problema con il seguente codice e non riesco a venirne a capo.
In pratica pur essendo presenti i dati richiesti non li trova.
Qui di seguito trovate il metodo definito nella classe che estende SQLiteOpenHelper e che mi genera il crash.
Ciò che vorrei fare è che per ogni tipo di fondamentale di ciascun giocatore calcoli quanti ne sono stati eseguiti. Ovviamente non è detto che tutti i giocatori eseguano tutti i fondamentali disponibili, quindi setto il valore per quel fondamentale a 0.
1
2public int getCountFondamentaleG(int idGiocatore, String fondamentale) {
3String selectQuery = "SELECT * FROM app_statistiche WHERE id_giocatore = " + idGiocatore + "
4AND tipo_fondamentale = " + fondamentale;
5SQLiteDatabase db = this.getReadableDatabase();
6Cursor cursor = db.rawQuery(selectQuery, null);
7int counter = 0;
8
9if(cursor != null)
10{
11cursor.moveToFirst();
12counter = cursor.getCount();
13}
14cursor.close();
15// return numero row
16return counter;
17}
18
Questa l'activity (il layout contiene semplicemente una textview statica, visualizzo su LogCat i risultati desiderati)
601
2public class StatisticheActivity extends Activity {
3/** Called when the activity is first created. */
4public void onCreate(Bundle savedInstanceState) {
5super.onCreate(savedInstanceState);
6setContentView(R.layout.main);
7
8
9DbHandler db = new DbHandler(this);
10List<Statistica> statistics = db.getAllRecords();
11
12for (Statistica stt : statistics) {
13String log = "Id: "+stt.getID()+" ,idGiocatore: " + stt.getIdGiocatore() + " ,Fondamentale: " +
14stt.getTipoFondamentale() + " ,Val: " + stt.getVal() + " ,set: " + stt.getNumSet();
15// Writing Contacts to log
16Log.d("Name: ", log);
17
18}
19
20int allRow = db.getStatsCount();
21Log.d("Numero righe: ", Integer.toString(allRow));
22
23//Cerco per ogni giocatore i fondamentali che ha fatto
24List<Statistica> lGiocatori = db.getGiocatore();
25for(Statistica stt : lGiocatori) {
26String tmp = "idGiocatore: " + stt.getIdGiocatore();
27Log.d("Trovato: ", tmp);
28//List<Statistica>lista = db.getFondamentale();
29List<Statistica>lista = db.getFondamentaleG(stt.getIdGiocatore());
30ArrayList<String> array = new ArrayList<String>();
31for(int i=0; i<lista.size(); i++)
32{
33array.add(lista.get(i).getTipoFondamentale());
34System.err.println(array.get(i));
35}
36}
37
38//stampo a video tutti i tipo_fondamentale in tabella
39List<Statistica> lFondamentali = db.getFondamentale();
40for(Statistica stt : lFondamentali) {
41String tmp = "tipoFondamentale: " + stt.getTipoFondamentale();
42Log.d("Trovato: ", tmp);
43}
44//Cerco per ogni giocatore per ogni fondamentale quanti ne ha fatti
45List<Statistica> lGiocatoriB = db.getGiocatore();
46List<Statistica> lFondamentaliB = db.getFondamentale();
47for(Statistica sttG : lGiocatoriB) {
48for(Statistica sttF : lFondamentaliB) {
49String tmp = "idGiocatore: " + sttG.getIdGiocatore() + " fondamentale: " + sttF.getTipoFondamentale();
50Log.d("Trovato: ", tmp);
51//List<Statistica>lista = db.getFondamentale();
52int count = db.getCountFondamentaleG(sttG.getIdGiocatore(), sttF.getTipoFondamentale());
53String tmp1 = "idGiocatore: " + sttG.getIdGiocatore() + "fondamentale: " +
54sttF.getTipoFondamentale() + "#: " + count;
55Log.d("Trovato: ", tmp1);
56}
57}
58}
59}
60