0

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:

LASTEST LOGCAT AFTER CHANGING THE EDIT TEXT ID's

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"/>

layout

2
  • try considering, realm, it's 5 times faster than sqlite and it's quite easy to use Commented Mar 4, 2016 at 4:22
  • Can u post activity_add_items.xml? Commented Mar 4, 2016 at 5:08

6 Answers 6

1

You can't give id "Location" to an editText.

 editTextAddress = (EditText) findViewById(R.id.Location);

change it to something else like "location" if you want.

Edit

 editTextFullName = (EditText) findViewById(R.id.textViewFullName);
    editTextAddress = (EditText) findViewById(R.id.editTextLocation);
Sign up to request clarification or add additional context in comments.

13 Comments

that is the id of my EditText for address.
@RoCk but "Location" is a class. you can't use it as a variable.
Yes, I already changed it to editTextLocation. :( still it doesn't, it return null.
@RoCk Can you provide your logcat output or some more info about ur code.
@RoCk you have provided wrong ids wait i am editing my answer.
|
0

Like others said You are giving wrong id of your textview, you need to change your xml editext id from editTextFullName to textViewFullName

or Change this statement

editTextFullName = (EditText) findViewById(R.id.editTextFullName);

with

editTextFullName = (EditText) findViewById(R.id.textViewFullName);

and just as a heads up in your "insert()" function in SQLiteLocalDatabase you might want to change

return sqLiteDatabase.insert(DB_NAME, null, values);

to

 return sqLiteDatabase.insert(DB_TABLE, null, values);

because you might end up with a android.database.sqlite.SQLiteException: no such table: project.db

1 Comment

Many Thank you, I can now add. I was confuse by my DB_NAME and DB_TABLE.
0

logcat showing NullPointerException at fullName string setting statement in below method (i.e onclick), so make sure editTextFullName textview is not null (having some value) before apply toString() and trim() methods on it. I'm suspecting select query not return all fields values properly. please try your query separately using some sqlite client tool to check actual result of your select query.

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();

Comments

0

Problem : It's like my EditText don't detect any value.

You are giving wrong id dear.. you need to change your java editext id from editTextFullName to textViewFullName

Change this statement

editTextFullName = (EditText) findViewById(R.id.editTextFullName);

with

editTextFullName = (EditText) findViewById(R.id.textViewFullName);

Comments

0

NullPointerException is because your EditText full name is null means can not find

I have figure out that your code for EditText for id is "textViewFullName" and you are getting id "editTextFullName"

just either you change the

editTextFullName = (EditText) findViewById(R.id.editTextFullName);

to

editTextFullName = (EditText) findViewById(R.id.textViewFullName);

-------------OR----------------------

<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"/>

to

<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/editTextFullName"
    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"/>

Hope this helps. If if solve your problem kindly accept it. Thank you.

Comments

0

Change editTextFullName = (EditText) findViewById(R.id.editTextFullName); to editTextFullName = (EditText) findViewById(R.id.textViewFullName);. You are referring to wrong ID, that's why it always null.

Add

sqLiteDatabase=this.getWritableDatabase();
ContentValues values=new ContentValues();

inside your insert method

1 Comment

Still, NullPointerException. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.