» »

[Android]Ne klice se onCreate in s tem se ne kreira baza

[Android]Ne klice se onCreate in s tem se ne kreira baza

KernelPanic ::

Spostovani!

Imam sledeco kodo:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.testdv;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 *
 * @author markofr
 */
public class CDatabaseAdapter
{
    public static final String COLUMN_ID="Id";
    public static final String COLUMN_NAME="Name";
    public static final String COLUMN_LATITUDE="Latitude";
    public static final String COLUMN_LONGITUDE="Longitude";

    private static final String DATABASE_NAME="db.testdv";
    private static final int DATABASE_VERSION=1;

    private static final String query_CREATE_DATABASE="CREATE TABLE "+DATABASE_NAME+
                                                      "("+COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+
                                                      COLUMN_NAME+"TEXT NOT NULL"+
                                                      COLUMN_LATITUDE+"TEXT NOT NULL"+
                                                      COLUMN_LONGITUDE+"TEXT NOT NULL";

    private static final String TABLE_LOCATIONS="evTest";

    private final Context context;
    private static DatabaseHelper dbhelper;
    private static SQLiteDatabase db;

    public CDatabaseAdapter(Context c)
    {
        this.context=c;
        dbhelper=new DatabaseHelper(context);
    }   // default constructor

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        private static final String TAG="DatabaseHelper";

        DatabaseHelper(Context context)
        {
            super(context,
                  DATABASE_NAME,
                  null,
                  DATABASE_VERSION);
        }   // default constructor

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            try
            {
                db.execSQL(query_CREATE_DATABASE);
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
            finally
            {
            }   // try-catch-finally block
        }   // onCreate

        @Override
        public void onUpgrade(SQLiteDatabase db,
                              int oldVersion,
                              int newVersion)
        {
            Log.w(TAG,
                  "Upgrading database from version "+oldVersion+
                  " to "+newVersion+
                  ", This will remove all previous data");
            db.execSQL("Drop TABLE IF EXISTS");
            onCreate(db);
        }   // onUpgrade

        public static SQLiteDatabase openWrite() throws SQLException
        {
            return dbhelper.getWritableDatabase();
        }   // openWrite

        public static SQLiteDatabase openRead() throws SQLException
        {
            return dbhelper.getReadableDatabase();
        }   // openRead

        public static void closeReadWrite()
        {
            dbhelper.close();
        }   // close

        public static long insertData(final String name,
                               final String latitude,
                               final String longitude)
        {
            ContentValues cv=new ContentValues();

            cv.put(COLUMN_NAME,
                   name);
            cv.put(COLUMN_LATITUDE,
                   latitude);
            cv.put(COLUMN_LONGITUDE,
                   longitude);

            return db.insert(TABLE_LOCATIONS,
                             null,
                             cv);
        }   // insertData
    }   // class DatabaseHelper
}   // class CDatabaseAdapter
Ko instanciram razred CDatabaseAdapter, se kreira DatabaseHelper in s tem se klice njegov konstruktor. V dokumentaciji pise, da se ob klicu konstruktorja DatabaseHelper-ja pozene tudi onCreate? Vendar, ce postavim breakpoint na ta onCreate, se metoda ne izvede in s tem (po moje) tudi baze se ni. Zakaj?

Lp,
M.

Spura ::

Kaj pise v dokumentaciji konstruktorja v SQLiteOpenHelper?

Create a helper object to create, open, and/or manage a database. This method always returns very quickly. The database is not actually created or opened until one of getWritableDatabase() or getReadableDatabase() is called.

KernelPanic ::

Prebral sm to na netu, oz. to kodo sem nasel na netu na stackoverflow-u in ne deluje najbolj, ne vem zakaj. Sedaj modificiram db adapter razred in javim sprebemembe! Samo, generalno gledano na Android SDK, ko kreiras objekt iz classa, se klice onCreate ali se motim? Ne vem, kje sem to prebral in me malo bega ...

Spura ::

Lej, pise ti, da se database kreira sele ob klicih tistih dveh metod, torej ne bo konstruktor klical onCreate.
Konstrukcija objekta z onCreate nima nic. Ce se je dizajner classa odlocil imel te callbacke je to prepusceno njegovi implementaciji in s tem tudi vsi detajli (kdaj se to klice).

Zgodovina sprememb…

  • spremenil: Spura ()

KernelPanic ::

Katerih dveh, mislis getWritable in getReadable par?

Zgodovina sprememb…



Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Android - Začetnik

Oddelek: Programiranje
111790 (1115) hurlimannxt
»

android črta

Oddelek: Programiranje
412410 (1681) g333kk
»

[android] vstavljanje slike

Oddelek: Programiranje
71247 (1144) messi
»

[android] crash

Oddelek: Programiranje
61343 (1152) messi
»

[Android]Software Design vprasanje

Oddelek: Programiranje
91934 (1754) KernelPanic

Več podobnih tem