TapkeysActivity.java 在“tapdb hon = new tapdb(this);”处出错 . 它告诉定义构造函数 tapdb(new View.OnClickListener(){})。请告诉我如何定义构造函数。相关代码如下:
TapkeysActivity.java
package com.android.tusway;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TapkeysActivity extends Activity {
private EditText et;
private Button b1,b2,b3,b4,bspace;
private String stringDb="";
private TextView tv;
private void updateTextField() {
tv.setText(this.stringDb);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// adjusting key regarding window sizes
et = (EditText) findViewById(R.id.editText1);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
bspace = (Button) findViewById(R.id.btspace);
tv = (TextView) findViewById(R.id.textView1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stringDb += "1";
updateTextField();
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stringDb += "2";
updateTextField();
}
});
b3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stringDb += "3";
updateTextField();
}
});
b4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stringDb += "4";
updateTextField();
}
});
bspace.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stringDb += " ";
updateTextField();
long l = Long.parseLong(stringDb);
tapdb hon = new tapdb(this);
hon.open();
String returnedWord = hon.getWord(l);
hon.close();
et.setText(returnedWord);
}
});
}
}
Tapdb.java
package com.android.tusway;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class tapdb {
public static final String KEY_ROWID="_id";
public static final String KEY_CODE="key_code";
public static final String KEY_WORD="word";
public static final String DATABASE_NAME="tapdatabase";
public static final String DATABASE_TABLE="tapData";
public static final int DATABASE_VERSION=1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context,DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE" + DATABASE_TABLE + "(" +
KEY_ROWID + "INTEGER PRIMARY KEY AUTOINCREAMENT,"+
KEY_WORD + "TEXT NOT NULL" +
KEY_CODE + "LONG);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE );
onCreate(db);
}
}
public tapdb (Context c) {
ourContext = c;
}
public tapdb open(){
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public String getWord(long l) {
String[] columns = new String[] {KEY_ROWID, KEY_WORD, KEY_CODE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String hotness = c.getString(1);
return hotness;
}
return null;
}
}