Android传感器工作

时间:2018-10-15 03:31:44

标签: android sqlite

UsersMaster类

public final class UserProfile {

    private UserProfile(){}

    public static final class Users implements BaseColumns{

        public static final String TABLE_NAME = "UserInfo";
        public static final String USERNAME = "userName";
        public static final String DOB = "dateOFBirth";
        public static final String PASSWORD = "password";
        public static final String GENDER = "Gender";
    }
}

数据库助手

public class DBHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "Users.db";

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

    @Override
    public void onCreate(SQLiteDatabase db) {

        String sqlQuery = "CREATE TABLE " + UserProfile.Users.TABLE_NAME + " (" +
                UserProfile.Users._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                UserProfile.Users.USERNAME + " TEXT," +
                UserProfile.Users.DOB + " TEXT," +
                UserProfile.Users.PASSWORD + " TEXT," +
                UserProfile.Users.GENDER + " TEXT)" ;

        db.execSQL(sqlQuery);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    }


    public boolean addInfo(String username, String dob, String password, String gender){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues con = new ContentValues();
        con.put(UserProfile.Users.USERNAME, username);
        con.put(UserProfile.Users.DOB, dob);
        con.put(UserProfile.Users.PASSWORD, password);
        con.put(UserProfile.Users.GENDER, gender);

        long r = db.insert(UserProfile.Users.TABLE_NAME, null, con);

        if(r == 1)
            return false;
        else
            return true;
    }


    public boolean updateInfo(String username, String dob, String password, String gender) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues con = new ContentValues();
        con.put(UserProfile.Users.DOB, dob);
        con.put(UserProfile.Users.PASSWORD, password);
        con.put(UserProfile.Users.GENDER, gender);

        String where = UserProfile.Users.USERNAME + " =?";
        String[] wheree = {username};

        long r = db.update(UserProfile.Users.TABLE_NAME, con, where, wheree);

        if (r == 1)
            return false;
        else
            return true;
    }


    public boolean deleteInfo(String username) {
        SQLiteDatabase db = this.getWritableDatabase();

        String where = UserProfile.Users.USERNAME + " =?";
        String[] wheree = {username};

        long r = db.delete(UserProfile.Users.TABLE_NAME, where, wheree);

        if (r == 1)
            return false;
        else
            return true;
    }


    public Cursor ReadAllInfo(){
        SQLiteDatabase db = this.getReadableDatabase();

        String[] column = {UserProfile.Users.USERNAME, UserProfile.Users.DOB, UserProfile.Users.PASSWORD, UserProfile.Users.GENDER};

        Cursor cursor = db.query(UserProfile.Users.TABLE_NAME, column, null, null, null, null, null);

        return cursor;
    }

    public Cursor ReadAllInfo(String username){
        SQLiteDatabase db = this.getReadableDatabase();

        String[] column = {UserProfile.Users.USERNAME, UserProfile.Users.DOB, UserProfile.Users.PASSWORD, UserProfile.Users.GENDER};

        String where = UserProfile.Users.USERNAME + " =?";
        String[] wheree = {username};

        Cursor cursor = db.query(UserProfile.Users.TABLE_NAME, column, where, wheree, null,null,null);

        return cursor;
    }


}

首页

public class Home extends AppCompatActivity {

    EditText un, pw;


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

        un = (EditText) findViewById(R.id.editText);
        pw = (EditText) findViewById(R.id.editText2);
    }

    public void LoginButtonClick(View view){
        int x=0;

        DBHelper db = new DBHelper(this);

        Cursor cursor = db.ReadAllInfo();

        List<String> list = new ArrayList<>();

        while(cursor.moveToNext()){
            if(un.getText().toString().equals(cursor.getString(0)) && pw.getText().toString().equals(cursor.getString(1))){

                x=1;
                break;
            }
        }

        if(x ==1){
            Intent intent = new Intent(this, EditProfile.class);
            startActivity(intent);

            Toast.makeText(this, "Successfully logged in", Toast.LENGTH_SHORT).show();

        }
        else{
            Toast.makeText(this, "Login failed", Toast.LENGTH_SHORT).show();
        }


    }

    public void RegisterButtonClick(View view){
        Intent intent = new Intent(this, profileManagement.class);
        startActivity(intent);
    }
}

EditProfile

public class EditProfile extends AppCompatActivity {

    EditText un, dob, pw;
    RadioGroup gender;
    RadioButton male,female;

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

        un = (EditText) findViewById(R.id.username1);
        dob = (EditText) findViewById(R.id.dob1);
        pw = (EditText) findViewById(R.id.password1);

        male = (RadioButton) findViewById(R.id.male);
        female = (RadioButton) findViewById(R.id.female);

    }


    public void UpdateButtonClick(View view){
        DBHelper db = new DBHelper(this);

        gender = (RadioGroup) findViewById(R.id.gender1);
        int x = gender.getCheckedRadioButtonId();
        RadioButton gender2 = (RadioButton) findViewById(x);

        boolean r = db.updateInfo(un.getText().toString(), dob.getText().toString(), pw.getText().toString(), gender2.getText().toString());

        if(r == false) {
            // Toast.makeText(this,"failed", Toast.LENGTH_SHORT).show();
        }
        else {
            // Toast.makeText(this, "successfull", Toast.LENGTH_SHORT).show();
        }
    }


    public void delete(View view){
        DBHelper db = new DBHelper(this);

        boolean r = db.deleteInfo(un.getText().toString());

        if(r == false) {
            //Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show();
        }
        else {
           // Toast.makeText(this, "Successful", Toast.LENGTH_SHORT).show();
        }
    }


    public void ReadAll(View view){
        DBHelper db = new DBHelper(this);

        List<String> list = new ArrayList<>();

        Cursor cursor = db.ReadAllInfo();

        while(cursor.moveToNext()){
            list.add(cursor.getString(0));
            list.add(cursor.getString(1));
            list.add(cursor.getString(2));
            list.add(cursor.getString(3));
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        CharSequence[] charSequences = list.toArray(new CharSequence[list.size()]);

        builder.setItems(charSequences, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.show();
    }

    public void ReadOne(View view){
        DBHelper db = new DBHelper(this);

        Cursor cursor = db.ReadAllInfo(un.getText().toString());

        while(cursor.moveToNext()){

            un.setText(cursor.getString(0));
            dob.setText(cursor.getString(1));
            pw.setText(cursor.getString(2));
            if(cursor.getString(3).equals("Male")){
                male.setChecked(true);
            }
            else {
                female.setChecked(true);
            }
        }
    }
}

在这里,我尝试访问sqlite数据库。

但是出了点问题。

0 个答案:

没有答案