I am a newbie to android studio and have replicated sample code samples to fit my application. The purpose is to read all my company records. I have tried a couple variations without success. The error I get indicates Customers() can not be applied to: with a list of the fields beneath. I have 12 tables with CRUD methods for each. The same error for all tables is the same.
I have all the individual table.java files built and want to finish all the CRUD functions without errors before moving on to the main activity.java file.
// Get All CompanyData =========================================================
public List<CompanyData> GetAllCompanyData() {
List<CompanyData> compdataList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_CompanyData;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
CompanyData compdata;
compdata = new CompanyData();
compdata.setcompanyid(Long.parseLong(cursor.getString(0))); // <==============================================================
compdata.setcompanyname(cursor.getString(1));
compdata.setcompanyaddress1(cursor.getString(2));
compdata.setcompanyaddress2(cursor.getString(3));
compdata.setcompanycity(cursor.getString(4));
compdata.setcompanystate(cursor.getString(5));
compdata.setcompanyzipcode(cursor.getString(6));
compdata.setcompanyphone(cursor.getString(7));
compdata.setcompanyemail(cursor.getString(8));
compdata.setcompanybusinesslicense(cursor.getString(9));
compdata.setcompanytype(cursor.getString(10));
compdataList.add(compdata);
} while (cursor.moveToNext());
}
return compdataList;
}
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:gravity="center"
android:text="Username"
android:textSize="18sp"
android:textStyle="bold|italic" />
<EditText
android:id="#+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView"
android:ems="10"
android:gravity="center_vertical|center"
android:hint="Enter Name"
android:inputType="textPersonName"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/editName"
android:layout_marginTop="13dp"
android:gravity="center"
android:hint="Enter Password"
android:text="password"
android:textSize="18sp"
android:textStyle="bold|italic" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button"
android:layout_alignBottom="#+id/button"
android:layout_alignEnd="#+id/button4"
android:layout_alignRight="#+id/button4"
android:onClick="viewdata"
android:text="view data"
android:textSize="18sp"
android:textStyle="bold|italic" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/editPass"
android:layout_marginLeft="28dp"
android:layout_marginStart="28dp"
android:layout_marginTop="23dp"
android:onClick="addUser"
android:text="add user"
android:textSize="18sp"
android:textStyle="bold|italic" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button4"
android:layout_alignStart="#+id/button4"
android:layout_below="#+id/editText3"
android:layout_marginTop="13dp"
android:onClick="update"
android:text="Update"
android:textStyle="normal|bold" />
<EditText
android:id="#+id/editText6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/button2"
android:layout_toStartOf="#+id/button2"
android:ems="10"
android:freezesText="false"
android:hint="Enter Name to Delete Data"
android:inputType="textPersonName" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="41dp"
android:layout_marginEnd="21dp"
android:layout_marginRight="21dp"
android:onClick="delete"
android:text="delete"
android:textStyle="normal|bold"
tools:ignore="RelativeOverlap" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button"
android:layout_marginLeft="7dp"
android:layout_marginStart="7dp"
android:layout_marginTop="47dp"
android:ems="10"
android:hint="Current Name"
android:inputType="textPersonName"
android:textSize="14sp"
android:textStyle="bold|italic" />
<EditText
android:id="#+id/editPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView2"
android:layout_marginTop="11dp"
android:ems="10"
android:gravity="center_vertical|center"
android:hint="Enter Password"
android:inputType="textPassword"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="normal|bold" />
<EditText
android:id="#+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_alignStart="#+id/editText3"
android:layout_alignTop="#+id/button3"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="New Name"
android:inputType="textPersonName"
android:textSize="14sp"
android:textStyle="bold|italic" />
</RelativeLayout>'
main activityjava
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText Name, Pass , updateold, updatenew, delete;
DbAdapter helper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name= (EditText) findViewById(R.id.editName);
Pass= (EditText) findViewById(R.id.editPass);
updateold= (EditText) findViewById(R.id.editText3);
updatenew= (EditText) findViewById(R.id.editText5);
delete = (EditText) findViewById(R.id.editText6);
helper = new DbAdapter(this);
}
public void addUser(View view)
{
String t1 = Name.getText().toString();
String t2 = Pass.getText().toString();
if(t1.isEmpty() || t2.isEmpty())
{
Message.message(getApplicationContext(),"Enter Both Name and Password");
}
else
{
long id = helper.insertData(t1,t2);
if(id<=0)
{
Message.message(getApplicationContext(),"Insertion Unsuccessful");
Name.setText("");
Pass.setText("");
} else
{
Message.message(getApplicationContext(),"Insertion Successful");
Name.setText("");
Pass.setText("");
}
}
}
public void viewdata(View view)
{
String data = helper.getData();
Message.message(this,data);
}
public void update( View view)
{
String u1 = updateold.getText().toString();
String u2 = updatenew.getText().toString();
if(u1.isEmpty() || u2.isEmpty())
{
Message.message(getApplicationContext(),"Enter Data");
}
else
{
int a= helper.updateName( u1, u2);
if(a<=0)
{
Message.message(getApplicationContext(),"Unsuccessful");
updateold.setText("");
updatenew.setText("");
} else {
Message.message(getApplicationContext(),"Updated");
updateold.setText("");
updatenew.setText("");
}
}
}
public void delete( View view)
{
String uname = delete.getText().toString();
if(uname.isEmpty())
{
Message.message(getApplicationContext(),"Enter Data");
}
else{
int a= helper.delete(uname);
if(a<=0)
{
Message.message(getApplicationContext(),"Unsuccessful");
delete.setText("");
}
else
{
Message.message(this, "DELETED");
delete.setText("");
}
}
}
}'
DbAdapterjava
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbAdapter extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "myDatabase"; // Database Name
private static final String TABLE_NAME = "myTable"; // Table Name
private static final int DATABASE_Version = 1; // Database Version
private static final String UID = "_id"; // Column I (Primary Key)
private static final String NAME = "Name"; //Column II
private static final String MyPASSWORD = "Password"; // Column III
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
" (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " VARCHAR(255) ," + MyPASSWORD + " VARCHAR(225));";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
private Context context;
public DbAdapter(Context context) {
super(context, DATABASE_NAME, null, DATABASE_Version);
this.context = context;
}
public long insertData(String name, String pass) {
SQLiteDatabase dbb = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME, name);
contentValues.put(MyPASSWORD, pass);
long id = dbb.insert(TABLE_NAME, null, contentValues);
return id;
}
public String getData() {
SQLiteDatabase db = getWritableDatabase();
String[] columns = {UID, NAME, MyPASSWORD};
Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
StringBuffer buffer = new StringBuffer();
while (cursor.moveToNext()) {
int cid = cursor.getInt(cursor.getColumnIndex(UID));
String name = cursor.getString(cursor.getColumnIndex(NAME));
String password = cursor.getString(cursor.getColumnIndex(MyPASSWORD));
buffer.append(cid + " " + name + " " + password + " \n");
}
return buffer.toString();
}
public int delete(String uname) {
SQLiteDatabase db = this.getWritableDatabase();
String[] whereArgs = {uname};
int count = db.delete(TABLE_NAME, NAME + " = ?", whereArgs);
return count;
}
public int updateName(String oldName, String newName) {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME, newName);
String[] whereArgs = {oldName};
int count = db.update(TABLE_NAME, contentValues, NAME + " = ?", whereArgs);
return count;
}
#Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
} catch (Exception e) {
Message.message(context, "" + e);
}
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
Message.message(context, "OnUpgrade");
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (Exception e) {
Message.message(context, "" + e);
}
}
}'
Messagejava
import android.content.Context;
import android.widget.Toast;
public class Message {
public static void message(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}'
Related
I have a login activity, registration activity, and profile activity. After I press the profile activity i want it to display user data in the profile.
I am trying to display data of sqlite database in profile activity in text view and edit text and update the data in the profile activity. Here is the code.
Here is my user profile xml.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".UserProfile">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#fece2f"
android:padding="20dp">
<ImageView
android:id="#+id/profile_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerVertical="true"
android:src="#drawable/profile_image" />
<TextView
android:id="#+id/Username_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/profile_image"
android:fontFamily="#font/alfa_slab_one"
android:includeFontPadding="false"
android:text="Ian Kipchumba"
android:textColor="#000"
android:textSize="20sp" />
<TextView
android:id="#+id/username_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/Username_profile"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/profile_image"
android:includeFontPadding="false"
android:text="ian kipchumba"
android:textSize="14sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/full_name_profile"
android:hint="User Name"
android:drawableLeft="#drawable/profile_icon"
android:drawablePadding="10dp"
android:layout_marginBottom="10dp"
android:text=""
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/admission_profile"
android:hint="Admission"
android:drawableLeft="#drawable/ic_adm"
android:drawablePadding="10dp"
android:layout_marginBottom="10dp"
android:text=""
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/password_profile"
android:hint="Password"
android:drawableLeft="#drawable/ic_lock"
android:drawablePadding="10dp"
android:layout_marginBottom="10dp"
android:text=""
/>
<Button
android:id="#+id/button_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fece2f"
android:fontFamily="#font/alfa_slab_one"
android:text="UPDATE"
android:onClick="run"/>
</LinearLayout>
</LinearLayout>
Here is my USERPROFILE.class
public class UserProfile extends AppCompatActivity {
DatabaseHelper db;
EditText name, admission, password;
TextView Username_profile, username_field;
Button btnviewUpdate;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
db = new DatabaseHelper(this);
//Hooks
name = (EditText) findViewById(R.id.full_name_profile);
admission = (EditText) findViewById(R.id.admission_profile);
password = (EditText) findViewById(R.id.password_profile);
Username_profile = (TextView) findViewById(R.id.Username_profile);
username_field = (TextView) findViewById(R.id.username_field);
btnviewUpdate = (Button) findViewById(R.id.button_update);
}
}
Here is my DATABASEHELPER.class
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME ="register.db";
public static final String TABLE_NAME ="registeruser";
public static final String COL_1 ="ID";
public static final String COL_2 ="username";
public static final String COL_3 ="admission";
public static final String COL_4 ="password";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE registeruser (ID INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT,admission TEXT, password TEXT)");
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
public long addUser(String username, String admission, String password){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("username",username);
contentValues.put("admission",admission);
contentValues.put("password",password);
long res = db.insert("registeruser",null,contentValues);
db.close();
return res;
}
public boolean checkUser(String username, String password){
String[] columns = { COL_1, COL_3};
SQLiteDatabase db = getReadableDatabase();
String selection = COL_2 + "=?" + " and " + COL_4 + "=?";
String[] selectionArgs = { username, password };
Cursor cursor = db.query(TABLE_NAME,columns,selection,selectionArgs,null,null,null);
int count = cursor.getCount();
cursor.close();
db.close();
if(count>0)
return true;
else
return false;
}
}
I'm working on a mobile app on Android Studio for a university project. However, I'm not able to modify the table name in my CRUD, and if I do, it will cause the app to crash.
Here is my code:
AdminMathsActivity.java:
package com.example.schoolapp.Admin;
import android.app.AlertDialog;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.schoolapp.R;
public class AdminMathsActivity extends AppCompatActivity {
DataBaseHelper2 peopleDB;
Button btnAddData, btnViewData,btnUpdateData,btnDelete;
EditText etName,etEmail,etTVShow,etID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_maths);
peopleDB = new DataBaseHelper2(this);
etID = (EditText) findViewById(R.id.etID);
etName = (EditText) findViewById(R.id.etNewName);
etEmail = (EditText) findViewById(R.id.etNewEmail);
etTVShow = (EditText) findViewById(R.id.etNewTVShow);
btnAddData = (Button) findViewById(R.id.btnAddData);
btnViewData = (Button) findViewById(R.id.btnViewData);
btnUpdateData = (Button) findViewById(R.id.btnUpdateData);
btnDelete = (Button) findViewById(R.id.btnDelete);
AddData();
ViewData();
UpdateData();
DeleteData();
}
public void AddData() {
btnAddData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = etName.getText().toString();
String email = etEmail.getText().toString();
String tvShow = etTVShow.getText().toString();
boolean insertData = peopleDB.addData(name, email, tvShow);
if (insertData == true) {
Toast.makeText(AdminMathsActivity.this, "Vos données ont bien été insérez!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(AdminMathsActivity.this, "Vos données n’ont pas été insérez, veuillez réessayer.", Toast.LENGTH_LONG).show();
}
}
});
}
public void ViewData(){
btnViewData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Cursor data = peopleDB.showData();
if (data.getCount() == 0) {
display("Erreur", "Aucune données n'a été insérez.");
return;
}
StringBuffer buffer = new StringBuffer();
while (data.moveToNext()) {
buffer.append("ID Note: " + data.getString(0) + "\n");
buffer.append("ID Eleve: " + data.getString(1) + "\n");
buffer.append("Intitule: " + data.getString(2) + "\n");
buffer.append("Note: " + data.getString(3) + "\n");
display("Toutes les données insérez :", buffer.toString());
}
}
});
}
public void display(String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void UpdateData(){
btnUpdateData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int temp = etID.getText().toString().length();
if (temp > 0) {
boolean update = peopleDB.updateData(etID.getText().toString(), etName.getText().toString(),
etEmail.getText().toString(), etTVShow.getText().toString());
if (update == true) {
Toast.makeText(AdminMathsActivity.this, "Vos données ont bien été modifiés!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(AdminMathsActivity.this, "Vos données n’ont pas été modifiés, veuillez réessayer.", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(AdminMathsActivity.this, "Veuillez entrer un ID pour effectuer une modification!", Toast.LENGTH_LONG).show();
}
}
});
}
public void DeleteData(){
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int temp = etID.getText().toString().length();
if(temp > 0){
Integer deleteRow = peopleDB.deleteData(etID.getText().toString());
if(deleteRow > 0){
Toast.makeText(AdminMathsActivity.this, "Vos données ont bien été supprimé!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(AdminMathsActivity.this, "Vos données n’ont pas été supprimé, veuillez réessayer.", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(AdminMathsActivity.this, "Veuillez entrer un ID pour effectuer une suppression!", Toast.LENGTH_LONG).show();
}
}
});
}
}
DataBaseHelper2.java code:
package com.example.schoolapp.Admin;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper2 extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "people.db";
public static final String TABLE_NAME = "people_table";
public static final String COL1 = "ID";
public static final String COL2 = "NAME";
public static final String COL3 = "EMAIL";
public static final String COL4 = "TVSHOW";
public DataBaseHelper2(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
" NAME TEXT, EMAIL TEXT, TVSHOW TEXT)";
db.execSQL(createTable);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String name, String email, String tvShow){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2,name);
contentValues.put(COL3,email);
contentValues.put(COL4, tvShow);
long result = db.insert(TABLE_NAME, null, contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
public Cursor showData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return data;
}
public boolean updateData(String id, String name, String email, String tvShow){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL1,id);
contentValues.put(COL2,name);
contentValues.put(COL3,email);
contentValues.put(COL4,tvShow);
db.update(TABLE_NAME, contentValues, "ID = ?", new String[] {id});
return true;
}
public Integer deleteData(String id){
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?", new String[] {id});
}
}
activity_admin_maths.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Admin.AdminMathsActivity">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnAddData"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="ID :"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/etID"
android:layout_width="391dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_alignEnd="#+id/btnUpdateData"
android:layout_alignRight="#+id/btnUpdateData"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_toRightOf="#+id/textView1"
android:hint="Utilisation pour modifier et supprimer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView1" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="ID de l'eleve"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etID" />
<EditText
android:id="#+id/etNewName"
android:layout_width="391dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView6"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_toRightOf="#+id/textView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Intitule"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etNewName" />
<EditText
android:id="#+id/etNewEmail"
android:layout_width="391dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Note"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etNewEmail" />
<EditText
android:id="#+id/etNewTVShow"
android:layout_width="391dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5" />
<Button
android:id="#+id/btnAddData"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="64dp"
android:layout_marginLeft="64dp"
android:layout_marginTop="20dp"
android:text="Ajouter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etNewTVShow" />
<Button
android:id="#+id/btnViewData"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:layout_toEndOf="#+id/btnAddData"
android:layout_toRightOf="#+id/btnAddData"
android:text="Visualiser"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etNewTVShow" />
<Button
android:id="#+id/btnUpdateData"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_below="#+id/etNewEmail"
android:layout_marginStart="64dp"
android:layout_marginLeft="64dp"
android:layout_marginTop="4dp"
android:layout_toEndOf="#+id/btnViewData"
android:layout_toRightOf="#+id/btnViewData"
android:text="Modifier"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnAddData" />
<Button
android:id="#+id/btnDelete"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:text="Supprimer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnViewData" />
</android.support.constraint.ConstraintLayout>
And here is the error log :
2019-03-01 15:41:51.277 10663-10663/com.example.schoolapp E/SQLiteLog: (1) no such table: people_table2
2019-03-01 15:41:51.282 10663-10663/com.example.schoolapp E/SQLiteDatabase: Error inserting EMAIL=Fonction TVSHOW=15/20 NAME=1
android.database.sqlite.SQLiteException: no such table: people_table2 (code 1 SQLITE_ERROR): , while compiling: INSERT INTO people_table2(EMAIL,TVSHOW,NAME) VALUES (?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1562)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
at com.example.schoolapp.Admin.DataBaseHelper2.addData(DataBaseHelper2.java:43)
at com.example.schoolapp.Admin.AdminMathsActivity$1.onClick(AdminMathsActivity.java:52)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Basically if I change the table name to people_table2 for example, data won't be inserted in SQLite.
Typically you would not modify a table name, that is unless you were modifying the structure and eventually renaming the renamed table back to it's original name.
However, you can use ALTER TABLE to rename a table. However, note that in doing so you would have to modify the App accordingly so that the changed table name is used throughout.
If you just want to change the table name used when the table is first created (simply change public static final String TABLE_NAME = "people_table"; to public static final String TABLE_NAME = "people_table2";), i.e. in the onCreate method, but after the App has been previously run using the older table name. Then the simplest way is to :-
Make the code changes,
Do 1 of the following
delete/clear the App's data or
uninstall the App
or in your case increase the database version (e.g. change super(context, DATABASE_NAME, null, 1); to super(context, DATABASE_NAME, null, 2);)
rerun the App.
Note that doing 2.1 or 2.2 above will lose any data that has been stored in the database. To preserve data is possible but is more complex. Using 2.3 will result in a new table being created with the old one remaining.
The reason is that the onCreate method only runs when the database itself is created.
Additional
As the syntax for the DROP TABLE statement is wrong. If you change the table name AND increase the version then the App will crash with an error like :-
2019-03-01 16:25:12.956 5963-5963/aaa.adminmaths E/AndroidRuntime: FATAL EXCEPTION: main
Process: aaa.adminmaths, PID: 5963
android.database.sqlite.SQLiteException: near "IF": syntax error (code 1 SQLITE_ERROR): , while compiling: DROP IF TABLE EXISTS people_table
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1769)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1698)
at aaa.adminmaths.DataBaseHelper2.onUpgrade(DataBaseHelper2.java:33)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:398)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:298)
at aaa.adminmaths.DataBaseHelper2.showData(DataBaseHelper2.java:54)
at aaa.adminmaths.AdminMathsActivity$2.onClick(AdminMathsActivity.java:65)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
That is because the syntax should be DROP TABLE IF EXISTS the_table_name_to_drop
I'm a complete newbie to Android development. My intention is to create an app which stores user inputs collected in EditText into an SQLite database and then displays them in next activity as a ListView. There are also some buttons involved, some of them are redundant as the app is still in its early stages.
When building the Gradle, there were no errors, also the logcat seemed to be completely OK with the whole thing. The first sign of trouble was checking the /data/data/[projectname] folder which didn't include any .db file or database directory. I have managed to make the transition between activities flawless, but since apparently there's no database in my project, nothing can populate the ListView.
I'm asking for some help determining what's wrong with my code.
MainActivity:
package com.example.peroalex.trackofworkinghours;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
MyDatabaseHelper databaseHelper;
Button ADD, DISPLAY, DELETE, MODIFY;
EditText DESCRIPTION, LOCATION, START, FINISH, COMMENT, ID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ADD = (Button) findViewById(R.id.addButton);
DISPLAY = (Button) findViewById(R.id.displayButton);
DELETE = (Button) findViewById(R.id.deleteButton);
MODIFY = (Button) findViewById(R.id.modifyButton);
DESCRIPTION = (EditText) findViewById(R.id.descriptionEdit);
LOCATION = (EditText) findViewById(R.id.locationEdit);
START = (EditText) findViewById(R.id.startEdit);
FINISH = (EditText) findViewById(R.id.finishEdit);
COMMENT = (EditText) findViewById(R.id.commentEdit);
ID = (EditText) findViewById(R.id.idEdit);
databaseHelper = new MyDatabaseHelper(this, null, null, 1);
ADD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
databaseAddData();
}
});
}
public void databaseAddData() {
String addDSC = DESCRIPTION.getText().toString();
String addLOC = LOCATION.getText().toString();
String addSTR = START.getText().toString();
String addFNS = FINISH.getText().toString();
String addCOM = COMMENT.getText().toString();
databaseHelper.addData(addDSC, addLOC, addSTR, addFNS, addCOM);
}
public void Action (View view) {
Intent intent = new Intent(MainActivity.this, showRecords.class);
startActivity(intent);
}
}
MyDatabaseHelper:
package com.example.peroalex.trackofworkinghours;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by peroalex on 4/1/18.
*/
public class MyDatabaseHelper extends SQLiteOpenHelper {
//Define database properties
private static final int database_version = 1;
private static final String database_name = "DatabaseRecords.db";
private static final String table_name = "tasks";
private static final String column_id = "_id";
private static final String column_desc = "description";
private static final String column_loc = "location";
private static final String column_strt = "start";
private static final String column_fnsh = "finish";
private static final String column_comm = "comment";
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, database_name, factory, database_version);
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//This code will be executed when creating a database, it includes the SQLite query which initiates a table
String query = " CREATE TABLE " + table_name + " ( "
+ column_id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ column_desc + " TEXT "
+ column_loc + " TEXT "
+ column_strt + " TEXT "
+ column_fnsh + " TEXT "
+ column_comm + " TEXT " + ");";
sqLiteDatabase.execSQL(query);
}
//This method is used for deleting data according to given ID
public void removeData(Integer ID) {
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
sqLiteDatabase.execSQL("DELETE FROM " + table_name + " WHERE " + column_id + " = " + ID + ";");
sqLiteDatabase.close();
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + table_name);
onCreate(sqLiteDatabase);
}
//This method is used for adding data to the SQLite database
public void addData(String dsc, String loc, String st, String fi, String comm) {
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(column_desc, dsc);
values.put(column_loc, loc);
values.put(column_strt, st);
values.put(column_fnsh, fi);
values.put(column_comm, comm);
sqLiteDatabase.insert(table_name, null, values);
sqLiteDatabase.close();
}
public Cursor getData() {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
Cursor data = sqLiteDatabase.rawQuery("SELECT * FROM " + table_name, null);
return data;
}
}
showRecords:
package com.example.peroalex.trackofworkinghours;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.ArrayList;
/**
* Created by peroalex on 4/2/18.
*/
public class showRecords extends AppCompatActivity {
ListView listView;
MyDatabaseHelper myDatabaseHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_records);
listView = (ListView) findViewById(R.id.displayDBListview);
myDatabaseHelper = new MyDatabaseHelper(this, null, null, 1);
displayDB();
}
public void displayDB() {
Cursor data = myDatabaseHelper.getData();
ArrayList<String> listData = new ArrayList<>();
while (data.moveToNext()) {
listData.add(data.getString(1));
}
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
listView.setAdapter(adapter);
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.peroalex.trackofworkinghours.MainActivity">
<EditText
android:id="#+id/descriptionEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="#drawable/ic_description_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="#string/description"
android:inputType="text"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.052" />
<EditText
android:id="#+id/locationEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="#drawable/ic_location_on_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="#string/location"
android:inputType="text"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.14" />
<EditText
android:id="#+id/startEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="#drawable/ic_work_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="#string/start"
android:inputType="time"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.249" />
<EditText
android:id="#+id/finishEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="#drawable/ic_work_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="#string/finish"
android:inputType="time"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.337" />
<EditText
android:id="#+id/commentEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="#drawable/ic_comment_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="#string/comments"
android:inputType="textMultiLine"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.448" />
<Button
android:id="#+id/addButton"
style="#android:style/Widget.Holo.Button"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.297"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.628" />
<Button
android:id="#+id/deleteButton"
style="#android:style/Widget.Holo.Button.Borderless"
android:layout_width="80sp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/delete"
android:textColor="#android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.291"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.934" />
<Button
android:id="#+id/displayButton"
style="#android:style/Widget.Holo.Button"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/display"
android:onClick="Action"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.702"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.628" />
<Button
android:id="#+id/modifyButton"
style="#android:style/Widget.Holo.Button.Borderless"
android:layout_width="80sp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/modify"
android:textColor="#android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.708"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.934" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/insert_an_id_to_either_modify_or_delete_the_query"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.731" />
<EditText
android:id="#+id/idEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:drawableStart="#drawable/ic_update_black_24dp"
android:fontFamily="serif"
android:hint="#string/id"
android:inputType="text"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.829" />
</android.support.constraint.ConstraintLayout>
activity_show_records:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/displayDBListview"
android:layout_width="344dp"
android:layout_height="551dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Your query to create the database is incorrect.You have forgotten to add , after TEXT, Hence your database was never created. Have a look at the code below
String query = " CREATE TABLE " + table_name + " ("
+ column_id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ column_desc + " TEXT,"
+ column_loc + " TEXT,"
+ column_strt + " TEXT,"
+ column_fnsh + " TEXT,"
+ column_comm + " TEXT" + ");";
sqLiteDatabase.execSQL(query);
}
I am new in android app developement. I tried to insert values to SQLite database through the code below ; and it is not working. I'm desperate!
Can anyone pleaaaaase help me???
this is DatabaseHelper.JAVA
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "agents.db";
private static final String TABLE_NAME = "agents";
private static final String COLUMN_ID = "id";
private static final String COLUMN_MATRICULE = "matricule";
private static final String COLUMN_NOM = "nom";
private static final String COLUMN_PRENOM = "prenom";
private static final String COLUMN_FONCTION = "fonction";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_PASS = "pass";
private static final String COLUMN_ADDRESS = "address";
private static final String COLUMN_CIN = "cin";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table agents (id integer primary key not null , " +
", matricule integer not null , nom text not null , prenom text not null , fonction text not null , email text not null , pass text not null , address text not null);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
public void insertAgent(Agent a) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from agents";
Cursor cursor = db.rawQuery(query , null);
int count = cursor.getCount();
values.put(COLUMN_ID , count);
values.put(COLUMN_MATRICULE, a.getMatricule());
values.put(COLUMN_NOM, a.getNom());
values.put(COLUMN_PRENOM, a.getPrenom());
values.put(COLUMN_FONCTION, a.getFonction());
values.put(COLUMN_EMAIL, a.getEmail());
values.put(COLUMN_PASS, a.getPass());
values.put(COLUMN_ADDRESS, a.getAddress());
values.put(COLUMN_CIN, a.getCin());
db.insert(TABLE_NAME, null, values);
db.close();
}
public String searchPass(String matricule)
{
db = this.getReadableDatabase();
String query = "select matricule, pass from "+TABLE_NAME;
Cursor cursor = db.rawQuery(query , null);
String a, b;
b = "not found";
if(cursor.moveToFirst())
{
do{
a = cursor.getString(0);
if(a.equals(matricule))
{
b = cursor.getString(1);
break;
}
}
while(cursor.moveToNext());
}
return b;
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
db.execSQL(query);
this.onCreate(db);
}
}
Agent.JAVA
public class Agent {
String matricule,nom,prenom,fonction,email,pass,address,cin;
public String getMatricule() {
return matricule;
}
public void setMatricule(String matricule) {
this.matricule = matricule;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getFonction() {
return fonction;
}
public void setFonction(String fonction) {
this.fonction = fonction;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCin() {
return cin;
}
public void setCin(String cin) {
this.cin = cin;
}
}
InscRire.JAVA
public class InscRire extends Activity {
DatabaseHelper helper = new DatabaseHelper(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sinscrire);
}
public void onSignUpClick(View v)
{
if(v.getId()== R.id.Bsinscrire)
{
EditText matricule = (EditText)findViewById(R.id.TFmatricule);
EditText nom = (EditText)findViewById(R.id.TFnom);
EditText prenom = (EditText)findViewById(R.id.TFprenom);
EditText fonction = (EditText)findViewById(R.id.TFfonction);
EditText email = (EditText)findViewById(R.id.TFemail);
EditText pass1 = (EditText)findViewById(R.id.TFpass1);
EditText pass2 = (EditText)findViewById(R.id.TFpass2);
EditText address = (EditText)findViewById(R.id.TFaddress);
EditText cin = (EditText)findViewById(R.id.TFcin);
String matriculestr = matricule.getText().toString();
String nomstr = nom.getText().toString();
String prenomstr = prenom.getText().toString();
String fonctionstr = fonction.getText().toString();
String emailstr = email.getText().toString();
String pass1str = pass1.getText().toString();
String pass2str = pass2.getText().toString();
String addressstr = address.getText().toString();
String cinstr = cin.getText().toString();
if(!pass1str.equals(pass2str))
{
//popup msg
Toast pass = Toast.makeText(InscRire.this , "Mot de passe incorrect!" , Toast.LENGTH_SHORT);
pass.show();
}
else
{
//insert the detailes in database
Agent a = new Agent();
a.setMatricule(matriculestr);
a.setNom(nomstr);
a.setPrenom(prenomstr);
a.setFonction(fonctionstr);
a.setEmail(emailstr);
a.setPass(pass1str);
a.setAddress(addressstr);
a.setCin(cinstr);
helper.insertAgent(a);
}
}
}
}
Main.JAVA
public class MainActivity extends ActionBarActivity {
DatabaseHelper helper = new DatabaseHelper(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onButtonClick(View v)
{
if(v.getId() == R.id.Bconnexion)
{
EditText a = (EditText)findViewById(R.id.TFmatricule);
String mat = a.getText().toString();
EditText b = (EditText)findViewById(R.id.TFpass);
String pass = b.getText().toString();
String mot_de_pass = helper.searchPass(mat);
if(pass.equals(mot_de_pass))
{
Intent i = new Intent(MainActivity.this, Display.class);
i.putExtra("Matricule",mat);
startActivity(i);
}
else
{
Toast temp = Toast.makeText(MainActivity.this , "Matricule et mot de passe incorrect!" , Toast.LENGTH_SHORT);
temp.show();
}
}
if(v.getId() == R.id.Binscrire)
{
Intent i = new Intent(MainActivity.this, InscRire.class);
startActivity(i);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Display.JAVA
public class Display extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
String matricule = getIntent().getStringExtra("Matricule");
TextView tv = (TextView)findViewById(R.id.TVmatricule);
tv.setText(matricule);
}
}
sinscrire.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Matricule "
android:id="#+id/textView1" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/TFmatricule"
android:inputType="number"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Nom"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/TFnom" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Prenom"
android:id="#+id/textView3" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/TFprenom" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Fonction"
android:id="#+id/textView4" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:id="#+id/TFfonction" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Email"
android:id="#+id/textView5" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/TFemail" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Mot de passe"
android:id="#+id/textView6" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/TFpass1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Confirmer mot de passe"
android:id="#+id/textView7" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/TFpass2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Address"
android:id="#+id/textView8" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:id="#+id/TFaddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Cin"
android:id="#+id/textView9" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"
android:ems="10"
android:id="#+id/TFcin" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sinscrire"
android:id="#+id/Bsinscrire"
android:onClick="onSignUpClick" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Main.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connexion"
android:id="#+id/Bconnexion"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="onButtonClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Matricule"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/TFmatricule"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Mot de passe"
android:id="#+id/textView3"
android:layout_below="#+id/TFmatricule"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/TFpass"
android:layout_below="#+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="S'inscrire ici"
android:id="#+id/Binscrire"
android:layout_below="#+id/Bconnexion"
android:layout_toRightOf="#+id/textView3"
android:layout_toEndOf="#+id/textView3"
android:onClick="onButtonClick" />
</RelativeLayout>
Display.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Welcome,"
android:id="#+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/emptyString"
android:id="#+id/TVmatricule" />
</LinearLayout>
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"/>
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);
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
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();
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);
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.
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