Good day I'm having a problem with EditText. I was following a tutorial about SQLite - tutorial. It's like my EditText don't detect any value. I even try to check them if they're empty. Here's my sample code:
AddItemsActivity.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) {
int id = Integer.parseInt(editTextIDNO.getText().toString().trim());
String fullName = editTextFullName.getText().toString().trim();
String location = editTextAddress.getText().toString().trim();
String emailAdd = editTextEmailAdd.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
switch (v.getId()){
case R.id.buttonAddContacts:
//IF result == -1
long result = sqLiteLocalDatabase.insert(id,fullName,location,emailAdd,password);
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();
}
}
And for database: SQLiteLocalDatabase .java
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 NOT NULL, "+ 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.isOpen()){
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) {
}
}
Logcat:
addItems.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/bg"
tools:context="project.app.elective.ccs.mobileappproject.activities.AddItemsActivity">
<include android:id="@+id/toolbar_extend"
layout="@layout/toolbar"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Full Name"
android:drawableLeft="@drawable/ic_action_icon"
android:drawableStart="@drawable/ic_action_icon"
android:textColorHint="@color/textPrimaryColor"
android:id="@+id/textViewFullName"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_below="@+id/toolbar_extend"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Location Address"
android:drawableLeft="@drawable/ic_action_icon"
android:drawableStart="@drawable/ic_action_icon"
android:textColorHint="@color/textPrimaryColor"
android:id="@+id/editTextLocation"
android:layout_below="@+id/textViewFullName"
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/textPrimaryColor"
android:drawableLeft="@drawable/ic_action_icon"
android:drawableStart="@drawable/ic_action_icon"
android:inputType="text"
android:hint="Email Address"
android:id="@+id/editTextEmailAddress"
android:layout_below="@+id/editTextLocation"
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_action_icon"
android:drawableStart="@drawable/ic_action_icon"
android:textColorHint="@color/textPrimaryColor"
android:hint="Password"
android:inputType="textPassword"
android:id="@+id/editTextPassword"
android:layout_below="@+id/editTextEmailAddress"
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_vertical_margin"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:textColor="@color/textPrimaryColor"
android:id="@+id/buttonAddContacts"
android:layout_marginRight="3dp"
android:layout_marginEnd="3dp"
style="?attr/borderlessButtonStyle"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:textColor="@color/textPrimaryColor"
android:id="@+id/buttonUpdate"
android:layout_below="@+id/buttonAddContacts"
android:layout_marginRight="3dp"
android:layout_marginEnd="3dp"
style="?attr/borderlessButtonStyle"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:textColor="@color/textPrimaryColor"
android:id="@+id/buttonSearch"
android:layout_below="@+id/buttonDelete"
android:layout_marginRight="3dp"
android:layout_marginEnd="3dp"
style="?attr/borderlessButtonStyle"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:textColor="@color/textPrimaryColor"
android:id="@+id/buttonDelete"
android:layout_below="@+id/buttonUpdate"
style="?attr/borderlessButtonStyle"/>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_action_icon"
android:hint="ID no, you can leave it!"
android:inputType="text"
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"/>

