我有一个onClick按钮,当用户点击时,它会从数据库助手类调用一个方法来执行搜索。但由于某些原因它似乎没有用,请帮助我。 :)这是我的代码,其中包含按钮:
public class LessonAlphabets extends Activity implements View.OnClickListener {
Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatabaseHelper db = new DatabaseHelper(this);
//some other unrelated code
}
public void onClick(View view) {
btn = (Button) view;
if(alphabetName.equals(alphabetHolder)){
DatabaseHelper dbHelper = new DatabaseHelper(this);
String text= dbHelper.getDescription(alphabetHolder);//this is the method to query
dbHelper.close();
textAlpha.setText(alphabetHolder + text);
}
这是数据库助手类:
public class DatabaseHelper extends SQLiteOpenHelper {
//declarations and other methods
//....
//....
public String getDescription(String s_name) {
String data = "";
String selectQuery = "SELECT " + KEY_DESC + " FROM " + TABLE_SIGN + " WHERE signName = '" + s_name + "'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
// get the data into array,or class variable
data = cursor.getString(cursor.getColumnIndex(KEY_DESC));
} while (cursor.moveToNext());
}
db.close();
return data;
}
}
最后这里是数据库表的类(不知道这个问题是否重要):
public class Table_Sign {
private int id;
private String signName;
private String signNameBM;
private String desc;
private String descBM;
private int category;
public Table_Sign(){}
public Table_Sign(String signName, String signNameBM, String desc, String descBM, int category) {
super();
this.signName = signName;
this.signNameBM = signNameBM;
this.desc = desc;
this.descBM = descBM;
this.category = category;
}
//------- getters & setters
// getting ID
public int getId(){
return this.id;
}
// setting id
public void setId(int id){
this.id = id;
}
// getting signName
public String getSignName(){
return this.signName;
}
// setting signName
public void setSignName(String signName){
this.signName = signName;
}
// getting signNameBM
public String getSignNameBM(){
return this.signNameBM;
}
// setting signNameBM
public void setSignNameBM(String signNameBM){
this.signNameBM = signNameBM;
}
// getting desc
public String getDesc(){
return this.desc;
}
// setting desc
public void setDesc(String desc){
this.desc = desc;
}
// getting descBM
public String getDescBM(){
return this.descBM;
}
// setting descBM
public void setDescBM(String descBM){
this.descBM = descBM;
}
// getting category
public int getCategory(){
return this.category;
}
// setting category
public void setCategory(int category){
this.category = category;
}
//------- getters & setters
@Override
public String toString() {
return "Table_Sign [id=" + id + ", signName=" + signName + ", signNameBM=" + signNameBM + ", desc=" + desc + ", descBM=" + descBM
+ ", category=" + category + "]";
}
}