美好的一天我因此而插入数据时遇到问题。 这是我的代码:
addItem.java
public class AddItemsActivity extends AppCompatActivity implements View.OnClickListener{
SQLiteLocalDatabase sqLiteLocalDatabase;
EditText editTextFullName;
EditText editTextAddress;
EditText editTextEmailAdd;
EditText editTextPassword;
EditText editTextIDNO;
Button btnSave;
Button btnUpdate;
Button btnSearch;
Button btnDelete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_items);
sqLiteLocalDatabase = new SQLiteLocalDatabase(AddItemsActivity.this);
//CALL FUNCTION
setUpWidgets();
}
public void setUpWidgets(){
editTextFullName = (EditText) findViewById(R.id.editTextFullName);
editTextAddress = (EditText) findViewById(R.id.Location);
editTextEmailAdd = (EditText) findViewById(R.id.editTextEmailAddress);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
editTextIDNO = (EditText) findViewById(R.id.editTextID);
btnSave = (Button) findViewById(R.id.buttonAddContacts);
btnSave.setOnClickListener(this);
btnUpdate = (Button) findViewById(R.id.buttonUpdate);
btnUpdate.setOnClickListener(this);
btnSearch = (Button) findViewById(R.id.buttonSearch);
btnSearch.setOnClickListener(this);
btnDelete = (Button) findViewById(R.id.buttonDelete);
btnDelete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonAddContacts:
//IF result == -1
long result = sqLiteLocalDatabase.insert(Integer.parseInt(getValue(editTextIDNO)),
getValue(editTextFullName),
getValue(editTextAddress),
getValue(editTextEmailAdd),
getValue(editTextPassword)
);
if(result == -1){
Toast.makeText(AddItemsActivity.this, "Error",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(AddItemsActivity.this, "Sucess Id:"+result,Toast.LENGTH_LONG).show();
}
break;
case R.id.buttonUpdate:
long update = sqLiteLocalDatabase.update(Integer.parseInt(getValue(editTextIDNO)),
getValue(editTextFullName),
getValue(editTextAddress),
getValue(editTextEmailAdd),
getValue(editTextPassword)
);
if(update == 0){
Toast.makeText(AddItemsActivity.this, "Error Updating",Toast.LENGTH_LONG).show();
}else
if(update == -1){
Toast.makeText(AddItemsActivity.this, "Updating",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(AddItemsActivity.this, "Error All data updated Id:"+update,Toast.LENGTH_LONG).show();
}
break;
case R.id.buttonSearch:
break;
case R.id.buttonDelete:
long delete = sqLiteLocalDatabase.delete(Integer.parseInt(getValue(editTextIDNO)));
if(delete == 0) {
Toast.makeText(AddItemsActivity.this, "Error Delete", Toast.LENGTH_LONG).show();
} else
{
Toast.makeText(AddItemsActivity.this, "Success Delete", Toast.LENGTH_LONG).show();
}
break;
}
}
public String getValue(EditText editText){
return editText.getText().toString().trim();
}
@Override
protected void onStart() {
super.onStart();
sqLiteLocalDatabase.setUpDb();
}
@Override
protected void onStop() {
super.onStop();
sqLiteLocalDatabase.closeTransactionDb();
}
}
对于数据库
public class SQLiteLocalDatabase extends SQLiteOpenHelper{
private SQLiteDatabase sqLiteDatabase;
private static final String DB_NAME = "project.db";
private static final int VERSION = 1;
public static final String DB_TABLE = " user ";
public static final String ID = " _id ";
public static final String FULL_NAME = " fullname ";
public static final String LOCATION = " location ";
public static final String EMAIL_ADD = " email ";
public static final String PASSWORD = " password ";
public SQLiteLocalDatabase(Context context) {
super(context, DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String queryTable = " CREATE TABLE " + DB_TABLE + "( " + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ FULL_NAME + " TEXT NOT NULL, " + LOCATION + " TEXT NOT NULL, " + EMAIL_ADD + " TEXT NOT NULL, " + PASSWORD + " TEXT NOT NULL" + " ) ";
db.execSQL(queryTable);
}
public void setUpDb(){
//TO OPEN DATABASE - RE-WRITABLE
sqLiteDatabase = getWritableDatabase();
}
public void closeTransactionDb(){
//CLOSE DB IF OPEN
if(sqLiteDatabase != null){
sqLiteDatabase.close();
}
}
//INSERT DATA
public long insert(int id,String fullname, String location,String email,String password){
//CONTENT VALUE contains name-value-pairs
ContentValues values = new ContentValues();
if(id != -1) {
values.put(ID,id);
values.put(FULL_NAME, fullname);
values.put(LOCATION, location);
values.put(EMAIL_ADD, email);
values.put(PASSWORD, password);
}
//Object Table, column, values
return sqLiteDatabase.insert(DB_NAME, null, values);
}
//UPDATE
public long update(int id, String fullname,String location,String email, String password){
//CONTENT VALUE contains name-value-pairs
ContentValues values = new ContentValues();
values.put(FULL_NAME,fullname);
values.put(LOCATION,location);
values.put(EMAIL_ADD,email);
values.put(PASSWORD,password);
//WHERE
String where = ID + "="+id;
//Object Table, values, destination-id
return sqLiteDatabase.update(DB_NAME, values, where, null);
}
//DELETE
//
public long delete(int id){
//WHERE
String where = ID + "="+id;
//Object Table, values, destination-id
return sqLiteDatabase.delete(DB_NAME, where, null);
}
public Cursor getAllRecords(){
String queryDB = "SELECT * FROM "+DB_TABLE;
return sqLiteDatabase.rawQuery(queryDB,null);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
additem_xml
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
**android:inputType="text"**
android:drawableLeft="@drawable/ic_action_icon"
android:hint="ID no, you can leave it!"
android:textColorHint="@color/textPrimaryColor"
android:ems="10"
android:id="@+id/editTextID"
android:layout_below="@+id/editTextPassword"
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"/>
这是我总是得到的错误信息。我认为EditText转换为Integer是这里的问题。 我甚至更改了inputType =“number / text”。
NumberFormatException: Invalid int:
答案 0 :(得分:0)
editText.toString()
不是输入EditText
的字符串。您需要使用getText():
//CREATE METHOD for EditText
public String getValue(EditText editText){
return editText.getText().toString().trim();
}