1

I'm trying to save user information in my application. I have Employee number editText and Company EditText. My employee number is not a required field so that means my table can have null values. My problem is when I am comparing if the employee number entered by the user already exist in my database, it also reads the null values when the user do not enter anything on the employee number editText. Can anyone help me do this please?

This is my code where I save all the employee number in an arraylist and remove all the null values

ArrayList<String> employeenumber = databaseAdapter.getEmployeeNumber();
employeenumber.removeAll(Collections.singleton(null));
employeenumber.removeAll(Collections.singleton(""));

What I wanna do is, if the user enter employee number, it will check if the arraylist contains it and if yes, it will all check the company corresponds to it. I need to trap duplicate entry. For example the employee number is 1234 and the company is ABCD, the program must not accept it. Thanks for anyone who could help me.

1 Answer 1

1

First check whether the user enter something on employee number editText. Then use int flag to compare it. Try the code below in your main activity:

int flag1 = 0;
if (!Emp.equals(""))
{
    ArrayList<String> CompanyandEmp = new ArrayList<String>();
    CompanyandEmp = databaseAdapter.getCompanyEmp(Emp);
    CompanyandEmp.removeAll(Collections.singleton(""));
    for(int i=0;i< CompanyandEmp.size();i++)
      {
        String cmpemp = CompanyandEmp.get(i).toString();
        if(cmpemp.equalsIgnoreCase(companyName))
          {
             flag1 = 1;
          }
    }
}

if (flag1 == 0)
{
//as per your requirement
}else{
     Toast.makeText(RegistrationActivity.this, "Account info already exist.", Toast.LENGTH_LONG).show();
}

Then in your databaseAdapter:

public ArrayList<String> getCompanyEmp(String emp) {

    SQLiteDatabase db = this.getReadableDatabase();
    ArrayList<String> companyemp = new ArrayList<String>();

    try {
       Cursor c = db.query("select CompName FROM projsitemast where Emp='"+emp+"'", null);
        if (c != null) {
            c.moveToFirst();
            for (int j = 0; j < c.getCount(); j++) {
                companyemp.add(c.getString(0));
                c.moveToNext();
            }
            c.close();
        }
        return companyemp;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return companyemp;
}
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.