0

I'm new to android. Here's one of my OnClickListeners. The thing is I don't know how to make it save when clicking the button. When I call createEventT(Event event), it says "non-static method cannot be referenced from a static context". Sometimes there's no error message, but the app crashes when clicking this button. Any ideas? Thanks.

OnClickListener:

OnClickListener doneT = new OnClickListener(){
    @Override
    public void onClick(View v) {
        event.setTitle(inputToday.toString());
        event.setYY(bearsCalendar.get(Calendar.YEAR));
        event.setMM(bearsCalendar.get(Calendar.MONTH));
        event.setDD(bearsCalendar.get(Calendar.DAY_OF_MONTH));
        dateToday.setText(EventDBAdapter.createEventT(event));
    }
};

And the event class:

public class Event {

int YY,MM,DD;
private String title;

public void setYY(int YY){this.YY=YY;}
public void setMM(int MM){this.MM=MM;}
public void setDD(int DD){this.DD=DD;}
public void setTitle(String title){this.title=title;};

public int getYY(){return YY;}
public int getMM(){return MM;}
public int getDD(){return DD;}
public String getTitle(){return title;}

}

public class EventDBAdapter{

private final Context mCtx;
static final String dbName="BearDatabase";
static final String eventTable="Events";
static final int dbVersion=1;
static final String colID="EventId";
static final String colTitle="Title";
static final String colDetails="Details";
static final String colYear="YY";
static final String colMonth="MM";
static final String colDay="DD";
static final String colHour="HH";
static final String colMinute="TT";
public DatabaseHelper mDbHelper;
public SQLiteDatabase mDb;


public EventDBAdapter(Context context){
    mCtx=context;
}

private static class DatabaseHelper extends SQLiteOpenHelper{
    DatabaseHelper(Context context){
        super(context,dbName,null,dbVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "+eventTable+" ("+colID+" INTEGER PRIMARY KEY , "
                +colTitle+" TEXT NOT NULL , "+colDetails+" TEXT , "
                +colYear+" INTEGER , "+colMonth+" INTEGER , "+colDay+" INTEGER , "
                +colHour+" INTEGER , "+colMinute+" INTEGER );");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + eventTable);
        onCreate(db);
    }
}

public EventDBAdapter open() throws SQLException{
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}
public void close() {
    mDbHelper.close();
}
public void upgrade(){
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();
    mDbHelper.onUpgrade(mDb, 1, 0);
}
public long createEventT(Event event){
    ContentValues values = new ContentValues();
    values.put(colTitle,event.getTitle());
    values.put(colYear,event.getYY());
    values.put(colMonth, event.getMM());
    values.put(colDay, event.getDD());
    return mDb.insert(eventTable,null,values);
}
public long createEventI(Event event){
    ContentValues values = new ContentValues();
    values.put(colTitle,event.getTitle());
    return mDb.insert(eventTable,null,values);
}

}

Update: Any problems in the database? After I made the changes, the whole thing crush.

Gabe Lyu
  • 13
  • 3

2 Answers2

0

change below to

    dateToday.setText(EventDBAdapter.createEventT(event));

with below and it will work.

    dateToday.setText(String.valueOf(EventDBAdapter.createEventT(event)));

I hope you are initializing event object before using it in onClick. if not, then initialize it like event = new Event(); before

event.setTitle(inputToday.toString());
AAnkit
  • 27,299
  • 12
  • 60
  • 71
0

Unless EventDBAdapter in your question is an instance of the EventDBAdapter class, then you are attempting to use a non-static method (public long createEventT()) in a static way (by accessing it through the class name).

To access it, you first need to create an instance of EventDBAdapter. One way to achieve this would probably be (assuming you are calling this method from inside an Activity):

long date = new EventDBAdapter(this).createEventT(event);
dateToday.setText(String.valueOf(date));

There are more answers on this topic here.

Community
  • 1
  • 1
PPartisan
  • 8,173
  • 4
  • 29
  • 48