Rory Rory - 2 months ago 21
Java Question

Accessing Methods from Another Class Android

I am creating an android application that will be using a SQLite database. I have created a separate class called

DBAdapter
that holds all the methods relating to the database.

My issue comes when I try to access these methods from the main activity. I am still learning but I would normally instantiate the
DBAdapter
class and then I would be able to reference the methods. However the approach I am taking isnt working. Below is the single line of how I am trying to instantiate the class and under that is the rest of the class.

The error I am getting is
cannot resolve method 'open()'


Instantiate Line and method

DBAdapter db = new DBAdapter(this);
db.open();


Main Class

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ContentFrameLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

final Context context = this;

private FloatingActionButton fab;

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

DBAdapter db = new DBAdapter(this);

db.open();

fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

final Dialog dialog = new Dialog(context);

dialog.setContentView(R.layout.dialog_box);
dialog.setTitle("Add new Location!");
dialog.show();

final EditText NameEdit = (EditText) findViewById(R.id.NameInput);
final EditText LatEdit = (EditText) findViewById(R.id.LatInput);
final EditText LongEdit = (EditText) findViewById(R.id.LongInput);
final EditText PhoneEdit = (EditText) findViewById(R.id.PhoneInput);
final Button OkButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
final Button CancelButton = (Button) dialog.findViewById(R.id.dialogButtonCancel);

OkButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
String RealName = NameEdit.getText().toString();

String Lat = LatEdit.getText().toString();
Float realLat = Float.parseFloat(Lat);

String Long = LongEdit.getText().toString();
Float realLong = Float.parseFloat(Long);

String Phone = PhoneEdit.getText().toString();
Double realPhone = Double.parseDouble(Phone);

//Use above to create a new Geofence


}
});

CancelButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
dialog.dismiss();
}
});

}

});

}
}


DBAdpater Class

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;

/**
* Created by Rory on 09/09/2016.
*/
public class DBAdapter {

private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;

//Database Version & name
private static final String DATABASE_NAME = "OpenSesame";
private static final int DATABASE_VERSION = 1;
private static final String TAG = "DBMain";

private static final String GEOTABLE = "geoTable";

private static final String KEY_ID = "_id";
private static final String KEY_NAME = "name";
private static final String KEY_LAT = "lat";
private static final String KEY_LONG = "long";
private static final String KEY_RADIUS = "radius";
private static final String KEY_PHONE = "phone";

private static final String CREATE_GEO_TABLE = "CREATE TABLE "
+ GEOTABLE + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTO INCREMENT,"
+ KEY_NAME + " TEXT,"
+ KEY_LAT + " FLOAT,"
+ KEY_LONG + " FLOAT,"
+ KEY_RADIUS + " INTEGER,"
+ KEY_PHONE + " INTEGER" + ")";


public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}

private class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

/**
* Method to create each of the tables defined above
* @param db
*/
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(CREATE_GEO_TABLE);
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* Method for updating the database
* @param db
* @param newVersion
* @param oldVersion
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}

public DatabaseHelper open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}

public void close() {
DBHelper.close();
}

public Cursor DeleteGeo(String name) {
return db.rawQuery("delete from geoTable where name = " + name, null);
}

public long AddGeo(String name, float lat, float lon, double phone, int radius) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_LAT, lat);
initialValues.put(KEY_LONG, lon);
initialValues.put(KEY_PHONE, phone);
initialValues.put(KEY_RADIUS, radius);

return db.insert(GEOTABLE, null, initialValues);

}

public Cursor GetAllGeos() {
return db.rawQuery("select * from geoTable order by name ASC", null);
}
}
}

Answer

Your DBAdapter class does not have the open() method. It is the private inner class DatabaseHelper that has that method. You have two options to expose open() to be able to call it the way you are expecting.

Option 1: add an open() method to DBAdapter that calls DatabaseHelper's open() method

public void open() {
    DBHelper.open();
}

Option 2: remove DatabaseHelper entirely, and have DBAdapter extends SQLiteOpenHelper instead

public class DBAdapter extends SQLiteOpenHelper {
    ...
    public DBAdapter(Context ctx) {
        this.context = ctx;
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // add all the code from inside DatabaseHelper below

    /**
     * Method to create each of the tables defined above
     * @param db
     */
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        try {
            db.execSQL(CREATE_GEO_TABLE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // you will have to change this method to work
    public DBAdapter open() throws SQLException
    {
        db = getWritableDatabase();
        return this;
    }

    ...
}