我是android的新手。这是我的一个OnClickListeners。问题是我不知道如何在点击按钮时保存它。当我调用createEventT(Event event)
时,它说“无法从静态上下文中引用非静态方法”。有时没有错误消息,但单击此按钮时应用程序崩溃。有任何想法吗?感谢。
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));
}
};
事件类:
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;}
}
公共类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);
}
}
更新: 数据库中的任何问题?在我做出改变之后,整个事情就粉碎了。
答案 0 :(得分:0)
更改为
dateToday.setText(EventDBAdapter.createEventT(event));
以下,它将起作用。
dateToday.setText(String.valueOf(EventDBAdapter.createEventT(event)));
我希望您在onClick中使用它之前初始化event
对象。如果没有,那么在
event = new Event();
event.setTitle(inputToday.toString());
答案 1 :(得分:0)
除非您的问题中的import csv from 'csv';
import byline from 'byline';
import {createReadStream} from 'fs';
var readStreamByLine = byline(createReadStream(file));
readStreamByLine.on('data', function(chunk) {
var string = chunk.toString();
console.log('string == ' + string);
var parser = csv.parse({
delimiter: '\t'
});
parser.write(string);
parser.on('readable', () => {
console.log('parser readable');
while (csvdata = parser.read()) {
console.log(csvdata);
}
});
});
是EventDBAdapter
类的实例,否则您尝试以静态方式使用非静态方法(EventDBAdapter
)(通过访问它)通过班级名称)。
要访问它,首先需要创建public long createEventT()
的实例。实现这一目标的一种方法可能是(假设您从EventDBAdapter
内部调用此方法):
Activity
此主题有更多答案here。