用户名遇到麻烦,说空

时间:2019-03-09 16:57:45

标签: java android sqlite toast

我得到了吐司null null,应该吐司:登录后显示“ Welcome Angelo(用户名)Dionisio(userSurname)”,但显示“ Welcome null null”

检查用户是否存在后,它应该从数据库中获取名称并显示给Toast并切换活动...

我的错误是什么?

并且我想在以后显示吐司之后将用户名,id,usersurname存储到共享的首选项中。

enter image description here

myLoginActivity.java

public class Login extends AppCompatActivity implements View.OnClickListener {

private EditText editTextEmailPhone;
private EditText editTextPassword;
private Button Login;
private ProgressDialog progressDialog;
Parent_DatabaseHelper mydb;
SQLiteDatabase sqLiteDatabase;
ParentModel parentModel;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);


    editTextEmailPhone = findViewById(R.id.input_username);
    editTextPassword = findViewById(R.id.input_password);
    findViewById(R.id.btn_login).setOnClickListener(Login.this);

    progressDialog = new ProgressDialog(this);

    mydb = new Parent_DatabaseHelper(this);

    sqLiteDatabase = mydb.getReadableDatabase();
}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.btn_login: {
            userLogin();
            break;
        }

    }


}


private void userLogin() {
    String email = editTextEmailPhone.getText().toString().trim();
    String password = editTextPassword.getText().toString().trim();

    if (email.isEmpty()) {
        editTextEmailPhone.setError("Email or Phone Number is required");
        editTextEmailPhone.requestFocus();
        return;
    }

    if (password.isEmpty()) {
        editTextPassword.setError("Password is required");
        editTextPassword.requestFocus();
        return;
    }
    if (password.length()<6 ){
        editTextPassword.setError("Minimum of length of password should be 6");
        editTextPassword.requestFocus();
        return;
    }

    else{
        progressDialog.setMessage("Please Wait...");
        progressDialog.show();

        boolean exists = mydb.userExistance(email, password);
        if(exists){
            progressDialog.dismiss();
            SharedPrefs.saveSharedSetting(this, "NoAccount", "false");
            Intent intent = new Intent(Login.this, Parent_Home.class);

            ArrayList<ParentModel> arrayList = new ArrayList<>();
            parentModel = new ParentModel();

            String s_parentID;
            String s_parentName;
            String s_parentSurname;

            s_parentID = parentModel.getID();
            s_parentName = parentModel.getName();
            s_parentSurname = parentModel.getSurname();

            Toast.makeText(this, "Welcome " + s_parentName + s_parentSurname, Toast.LENGTH_SHORT).show();
            startActivity(intent);
            finish();
        }
        else {
            Toast.makeText(getApplicationContext(), "Login error", Toast.LENGTH_SHORT).show();
            progressDialog.dismiss();
            return;
        }
    }
}

public void Login_AdminParent(View view) {
    startActivity(new Intent(this, Login_AdminTeacher.class));
}
}

DatabaseHelper.java(如果需要)

package edu.angelo.parentsportal;

public class Parent_DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "Parents_Portal.db";
public static final String TABLE_NAME = "Parents_Table";
public static final String COL_0 = "ID";
public static final String COL_1 = "NAME";
public static final String COL_2 = "SURNAME";
public static final String COL_3 = "EMAIL_ADDRESS";
public static final String COL_4 = "PHONE_NUMBER";
public static final String COL_5 = "PASSWORD";

public Parent_DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, EMAIL_ADDRESS TEXT, PHONE_NUMBER TEXT, PASSWORD TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}

public boolean insertData(String name, String surname, String email_address, String phone_number, String password){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1,name);
    contentValues.put(COL_2,surname);
    contentValues.put(COL_3,email_address);
    contentValues.put(COL_4,phone_number);
    contentValues.put(COL_5,password);
    long result = db.insert(TABLE_NAME, null , contentValues);
    if (result == -1) {
        return false;
    }
    else {
        return true;
    }
}

public ArrayList<ParentModel> getAllParentsData(){
    ArrayList<ParentModel> list = new ArrayList<>();
    String sql = "select * from " + TABLE_NAME;
    SQLiteDatabase mydb = this.getWritableDatabase();
    Cursor cursor = mydb.rawQuery(sql, null);
    if (cursor.moveToFirst()) {
        do {
            ParentModel parentModel = new ParentModel();
            parentModel.setID(cursor.getString(0));
            parentModel.setName(cursor.getString(1));
            parentModel.setSurname(cursor.getString(2));
            parentModel.setEmail(cursor.getString(3));
            parentModel.setPhone_number(cursor.getString(4));
            parentModel.setPassword(cursor.getString(5));
            list.add(parentModel);
        }
        while (cursor.moveToNext());
    }
    return list;
}

public void updateData(int id, String name , String surname , String email , String phone_number , String password){
   ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1, name);
    contentValues.put(COL_2, surname);
    contentValues.put(COL_3, email);
    contentValues.put(COL_4, phone_number);
    contentValues.put(COL_5, password);

    SQLiteDatabase mydb = this.getWritableDatabase();
    mydb.update(TABLE_NAME, contentValues, COL_0 + "=" + id, null);
    mydb.close();
}

public void deleteParent(int id){
    SQLiteDatabase mydb = this.getWritableDatabase();
    mydb.delete(TABLE_NAME, COL_0 + "=" + id, null);
    mydb.close();
}

public boolean userExistance(String emailOrPhone, String pwd) {
    String sql = "select * from " + TABLE_NAME + " where (" + COL_3 + " = '" + emailOrPhone + "' OR " + COL_4 + " = '" + emailOrPhone + "') AND " + COL_5 + " = " + pwd;
    SQLiteDatabase mydb = this.getWritableDatabase();
    Cursor cursor = mydb.rawQuery(sql, null);
    ArrayList<ParentModel> list = new ArrayList<>();
    if (cursor.getCount() > 0) {
        if (cursor.moveToFirst()) {
            do {
                ParentModel parentModel = new ParentModel();
                parentModel.setID(cursor.getString(0));
                parentModel.setName(cursor.getString(1));
                parentModel.setSurname(cursor.getString(2));
                parentModel.setEmail(cursor.getString(3));
                parentModel.setPhone_number(cursor.getString(4));
                parentModel.setPassword(cursor.getString(5));
                list.add(parentModel);
            }
            while (cursor.moveToNext());
        }
        return true;
    }

    else{
        return false;
    }

}
}

1 个答案:

答案 0 :(得分:0)

此处的代码创建了ParentModel实例,但未设置其成员。

    parentModel = new ParentModel();

    String s_parentID;
    String s_parentName;
    String s_parentSurname;

    s_parentID = parentModel.getID();
    s_parentName = parentModel.getName();
    s_parentSurname = parentModel.getSurname();

所以s_parentNames_parentSurname为空。

要解决您的问题,您应该在调用ParentModel之前创建userExistance实例并将其传递给userExistance。例如:

. . .
parentModel = new ParentModel();
boolean exists = mydb.userExistance(email, password, parentModel);
. . .

然后更改userExistance。添加ParentModel参数并像这样删除parentModel = new ParentModel();

public boolean userExistance(String emailOrPhone, String pwd, ParentModel parentModel)
{
  String sql = "select * from " + TABLE_NAME + " where (" + COL_3 + " = '" + emailOrPhone + "' OR " + COL_4 + " = '" + emailOrPhone + "') AND " + COL_5 + " = " + pwd;
  SQLiteDatabase mydb = this.getWritableDatabase();
  Cursor cursor = mydb.rawQuery(sql, null);
  ArrayList<ParentModel> list = new ArrayList<>();
  if (cursor.getCount() > 0) {
    if (cursor.moveToFirst()) {
        do {
            parentModel.setID(cursor.getString(0));
            parentModel.setName(cursor.getString(1));
            parentModel.setSurname(cursor.getString(2));
            parentModel.setEmail(cursor.getString(3));
            parentModel.setPhone_number(cursor.getString(4));
            parentModel.setPassword(cursor.getString(5));
            list.add(parentModel);
        }
        while (cursor.moveToNext());
    }
    return true;
}
 return false;

}