我是 Android 开发的新手。我正在尝试在 sqllite 中创建一个数据库表并为其添加值。我使用以下代码
添加.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="@+id/nametxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/notxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
<Button
android:id="@+id/addcontactBtn"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="addContacts"
android:text="Add New" />
</LinearLayout>
数据库.java
public class DataBase extends SQLiteOpenHelper {
public static final String DETAILS = "Details";
public static final String DETAIL_ID = "_id";
public static final String DETAIL_NAME = "_name";
public static final String DETAIL_NO="_no";
private static final String DATABASE_NAME = "Details.db";
private static final int DATABASE_VERSION = 1;
// creation SQLite statement
private static final String DATABASE_CREATE = "create table " + DETAILS
+ "(" + DETAIL_ID + " integer primary key autoincrement, "
+ DETAIL_NAME +" text not null"+ DETAIL_NO + " text not null);";
public DataBase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " +DETAILS);
onCreate(db);
}
}
细节.java
public class Detail {
private int id;
private String name;
private String number;
public long getId() {return id; }
public void setId(int id) {this.id = id;}
public String getName() {return this.name;}
public void setName(String name) {this.name = name;}
public String getNumber(){return this.number; }
public void setnumber(String number){ this.number=number; }
}
DetailOperations.java
public class DetailOperations {
private DataBase dbHelper;
private String[] DETAILS_TABLE_COLUMNS = { DataBase.DETAIL_ID, DataBase.DETAIL_NAME,DataBase.DETAIL_NO };
private SQLiteDatabase database;
public DetailOperations(Context context) {
dbHelper = new DataBase(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase(); }
public void close() { dbHelper.close(); }
public Detail addDetail(String name,String number) {
ContentValues values = new ContentValues();
values.put(DataBase.DETAIL_NAME, name);
values.put(DataBase.DETAIL_NO, number);
long detId = database.insert(DataBase.DETAILS, null, values);
Cursor cursor = database.query(DataBase.DETAILS,
DETAILS_TABLE_COLUMNS, DataBase.DETAIL_ID + " = "
+ detId, null, null, null, null);
cursor.moveToFirst();
Detail newdetail = parseDetail(cursor);
cursor.close();
return newdetail;
}
private Detail parseDetail(Cursor cursor) {
Detail d = new Detail();
d.setId((cursor.getInt(0)));
d.setName(cursor.getString(1));
d.setnumber(cursor.getString(2));
return d;
} }
添加.java
public class Add extends Activity {
private DetailOperations detailDBoperation;
@Override
public void onCreate(Bundle savedInstaceState){
super.onCreate(savedInstaceState);
setContentView(R.layout.add);
}
public void addDetails(View view){
detailDBoperation=new DetailOperations(this);
detailDBoperation.open();
EditText name=(EditText) findViewById(R.id.nametxt);
EditText number=(EditText) findViewById(R.id.notxt);
detailDBoperation.addDetail(name.getText().toString(), number.getText().toString());
name.setText("");
number.setText("");
}}
当我按下“添加新”按钮时,我收到错误消息,不幸的是应用程序已停止并显示上一页。我在 DDMS 中检查它显示已创建数据库表,但未在表中添加值。
LogCat 显示
09-23 04:50:17.914: E/AndroidRuntime(10574): java.lang.IllegalStateException:
Could not find a method addContacts(View) in the activity class com.example.experiments.Add for onClick handler on view class android.widget.Button with id 'addcontactBtn'