Good day I'm having a problem inserting a data because of this. Here's my code:
addItem.java
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;}
}
//CREATE METHOD for EditText public String getValue(EditText editText){ return editText.toString().trim();
For the database
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"/>
THIS is the error message I always get. I think the EditText convert to Integer is the problem here. I even changed the inputType ="number/text".
NumberFormatException: Invalid int: "android.support.v7.widget.AppCompatEditText{5292013c VFED..CL .F...... 32,568-736,674 #7f0c0072 app:id/editTextID}"
Aucun commentaire:
Enregistrer un commentaire