Autore Topic: estrazione di tutte le righe da una tabella su ListActivity  (Letto 292 volte)

Offline dennypao

  • Nuovo arrivato
  • *
  • Post: 3
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Galaxy SII
  • Sistema operativo:
    4.1.2
estrazione di tutte le righe da una tabella su ListActivity
« il: 15 Settembre 2014, 18:19:01 CEST »
0
Buonasera a tutti,
vorrei realizzare una ListActivity che visualizza tutte le righe di una tabella estraendo il solo campo "data".

questo il codice dell'activity in questione

Codice (Java): [Seleziona]
public class AllRecords extends ListActivity{
       
        private DBManager db;  

        public String[] allRecipes()
        {
           ArrayList<String> results = new ArrayList<String>();
           Cursor cursor = db.rawQuery();
           if(cursor.moveToFirst())
           {
               do
               {
                   results.add(cursor.getString(3));
               }while(cursor.moveToNext());
               if(cursor != null && !cursor.isClosed())
                  cursor.close();
           }  
           String[] listString = new String[results.size()];
           listString = results.toArray(listString);
           return listString;
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, allRecipes());
                setListAdapter(adapter);       
                allRecipes();
            android.app.ActionBar actionBar = getActionBar();
            actionBar.show();
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }      
}

in un altro package ho definito le tre classi che si occupano del db

DBString
Codice (Java): [Seleziona]
package com.aandd.simbaproject.db;

public class DBStrings {
       
        public static final String FIELD_ID="_id";
    public static final String FIELD_NAME="nome";
    public static final String FIELD_INTENSITY="intensita";
    public static final String FIELD_DATE="data";
    public static final String FIELD_PATH="percorso_file";
    public static final String TBL_NAME="Simba";

}

DBHelper

Codice (Java): [Seleziona]
package com.aandd.simbaproject.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper
{
    public static final String DBNAME="SIMBA_DB";
 
    public DBHelper(Context context) {
        super(context, DBNAME, null, 1);
    }
 
    @Override
    public void onCreate(SQLiteDatabase db)  
    {
        String q="CREATE TABLE "+DBStrings.TBL_NAME+
                " ( _id INTEGER PRIMARY KEY AUTOINCREMENT," +
                DBStrings.FIELD_NAME+" TEXT," +
                DBStrings.FIELD_INTENSITY+" TEXT," +
                DBStrings.FIELD_PATH+" TEXT," +
                DBStrings.FIELD_DATE+" TEXT)";
        db.execSQL(q);
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {  }
 
}

e DBManager
Codice (Java): [Seleziona]
package com.aandd.simbaproject.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class DBManager
{
    private DBHelper dbhelper;
 
    public DBManager(Context ctx)
    {
        dbhelper=new DBHelper(ctx);
    }
   
        public void save(String name, String intensity, String date , String path)
    {
        SQLiteDatabase db=dbhelper.getWritableDatabase();
 
        ContentValues cv=new ContentValues();
        cv.put(DBStrings.FIELD_NAME, name);
        cv.put(DBStrings.FIELD_INTENSITY, intensity);
        cv.put(DBStrings.FIELD_PATH, path);
        cv.put(DBStrings.FIELD_DATE, date);
        try
        {
            db.insert(DBStrings.TBL_NAME, null,cv);
        }
        catch (SQLiteException sqle)
        {
            // Gestione delle eccezioni
        }
    }
 
    public boolean delete(long id)
    {
        SQLiteDatabase db=dbhelper.getWritableDatabase();
        try
        {
            if (db.delete(DBStrings.TBL_NAME, DBStrings.FIELD_ID+"=?", new String[]{Long.toString(id)})>0)
                return true;
            return false;
        }
        catch (SQLiteException sqle)
        {
            return false;
        }
 
    }
 
    public Cursor query()
    {
        Cursor crs=null;
        try
        {
            SQLiteDatabase db=dbhelper.getReadableDatabase();
            crs=db.query(DBStrings.TBL_NAME, null, null, null, null, null, null, null);
        }
        catch(SQLiteException sqle)
        {
            return null;
        }
        return crs;
    }
   
    public Cursor rawQuery(){
        Cursor crs=null;
        String selectQuery = "SELECT * FROM " + DBStrings.TBL_NAME;
        try
        {
            SQLiteDatabase db=dbhelper.getReadableDatabase();
            crs=db.rawQuery(selectQuery, null);
        }
        catch(SQLiteException sqle)
        {
            return null;
        }
        return crs;
    }
 
}

L'applicazione crasha quando esegue

Codice (Java): [Seleziona]
           Cursor cursor = db.rawQuery();
questo il logcat

Codice: [Seleziona]
09-15 16:17:40.736: E/AndroidRuntime(621): FATAL EXCEPTION: main
09-15 16:17:40.736: E/AndroidRuntime(621): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simbaproject/com.aandd.simbaproject.activity.AllRecordsActivity}: java.lang.NullPointerException
09-15 16:17:40.736: E/AndroidRuntime(621):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-15 16:17:40.736: E/AndroidRuntime(621):         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-15 16:17:40.736: E/AndroidRuntime(621):         at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-15 16:17:40.736: E/AndroidRuntime(621):         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-15 16:17:40.736: E/AndroidRuntime(621):         at android.os.Handler.dispatchMessage(Handler.java:99)
09-15 16:17:40.736: E/AndroidRuntime(621):         at android.os.Looper.loop(Looper.java:137)
09-15 16:17:40.736: E/AndroidRuntime(621):         at android.app.ActivityThread.main(ActivityThread.java:4745)
09-15 16:17:40.736: E/AndroidRuntime(621):         at java.lang.reflect.Method.invokeNative(Native Method)
09-15 16:17:40.736: E/AndroidRuntime(621):         at java.lang.reflect.Method.invoke(Method.java:511)
09-15 16:17:40.736: E/AndroidRuntime(621):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-15 16:17:40.736: E/AndroidRuntime(621):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-15 16:17:40.736: E/AndroidRuntime(621):         at dalvik.system.NativeStart.main(Native Method)
09-15 16:17:40.736: E/AndroidRuntime(621): Caused by: java.lang.NullPointerException
09-15 16:17:40.736: E/AndroidRuntime(621):         at com.aandd.simbaproject.activity.AllRecordsActivity.allRecipes(AllRecordsActivity.java:55)
09-15 16:17:40.736: E/AndroidRuntime(621):         at com.aandd.simbaproject.activity.AllRecordsActivity.onCreate(AllRecordsActivity.java:97)
09-15 16:17:40.736: E/AndroidRuntime(621):         at android.app.Activity.performCreate(Activity.java:5008)
09-15 16:17:40.736: E/AndroidRuntime(621):         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-15 16:17:40.736: E/AndroidRuntime(621):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

Ho dovuto adattare il codice che ho trovato negli esempi con il mio e probabilmente ho fatto qualche casino, potete aiutarmi?
Grazie in anticipo

Offline Sakazaki

  • Utente normale
  • ***
  • Post: 396
  • Respect: +74
    • Mostra profilo
  • Dispositivo Android:
    Sony xperia Z
  • Play Store ID:
    Saka Labs
  • Sistema operativo:
    Windows 8
Re:estrazione di tutte le righe da una tabella su ListActivity
« Risposta #1 il: 16 Settembre 2014, 16:48:30 CEST »
0
Codice (Java): [Seleziona]
Cursor cursor = db.rawQuery();
Difficile che db sia != null se non viene mai istanziato.

Codice (Java): [Seleziona]
db = new DBManager(this);
Cursor cursor = db.rawQuery();

Potrebbe essere già un buon inizio.