Call function from another activity

I have an activity that contains all the functions for managing my database, and I want all other actions to be used for these functions when interacting with the database. Below is an example of my problem.

Watch script:

public class Clock extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clock);

        Data.createTable(); //<<<
    }
        //...
}

Data script:

public class Data extends Activity
{
    SQLiteDatabase mydb;
    private static String DBNAME = "SHIFTS.db";
    private static String TABLE = "MY_SHIFTS"; 

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data);
    }   

    public void createTable() //<<<
    {
        try
        {
            mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
            mydb.execSQL("CREATE TABLE IF  NOT EXISTS "+ TABLE +" (ID INTEGER PRIMARY KEY, STARTDATE TEXT, ENDDATE TEXT, LENGTH INTEGER, TYPE INTEGER);");
            mydb.close();
        }
        catch(Exception e)
        {
            Toast.makeText(getApplicationContext(), "Error in creating table", Toast.LENGTH_LONG).show();
        }
    }
    // ... lots more functions
}

Error message:

It is not possible to make a static reference to the non-static createTable () method on a data type.

And when I try to make the method static, it just causes more problems.

Also, if I try Data data = new Data (); data.createTable (); I get a NullPointerException.

What is the problem?

+4
source share
4 answers

static

public static void createTable()

:

Data.createTable();

- //.

.

, "" - , , .

dbTableCreate:

// Create the database table if it doesn't exist
protected final static void dbTableCreate(final Context ctx)
{
    SQLiteDatabase db = null;
    try
    {
        db = ctx.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
        db.execSQL
        (
            "CREATE TABLE IF NOT EXISTS " + DB_TABLE +
            " (date DATETIME PRIMARY KEY NOT NULL DEFAULT (CURRENT_DATE), " +
            "score INTEGER DEFAULT (null));"
        );
    }
    catch (final SQLiteException se)
    {
        System.out.println
        (
            ctx.getClass().getSimpleName() + "\n" +
                "Could not create the database"
        );
        se.printStackTrace();
    }
    finally
    {
        db.close();
    }
}

:

Context ctx = getApplicationContext();
CLS_DB.dbTableCreate(ctx);

,

public final class CLS_DB
{
    /* ----------------------------- Constants ------------------------------ */

    private final static String DB_NAME = "pat.db";
    private final static String DB_TABLE = "tests";

    /* ------------------------------ Methods ------------------------------- */

.
, .
( , ).

+12

java- - Singleton class Database.Put . Singleton.

0

i , Android_Dev,

1-

 class blah extends activity {
 //// onCreate , initialization etc here

     public void sss(context c)
   {

    Toast(c , "text" , toast.LENTGH_LONG) ;

   }

  }

public xxxx extends activity
{

Context c = getApplicationContext();
blah b = new blah(this) ;

 /// inside an onclicklistener attached to a button object, i put this
    b.sss(c) ; 

}

I did not include all the other things commonly found in classes extending the Activity class, as I just wanted to show what I did to call the function from another class. This solution was perfect for me. May or may not be useful to others, but

-2
source

All Articles