我是业余爱好者,需要帮助。
我想使用现有的SQLite数据库制作Spinner,例如:my spinner layout
我已成功在SQLite数据库上使用“用户”表进行了登录部分的操作(来自youtube的教程)。
这是DBHelper.java
类:
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "danisflora.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "kategori";
private final Context context;
SQLiteDatabase db;
private static final String DATABASE_PATH = "/data/data/com.abs.danisflora/databases/";
private final String USER_TABLE = "user";
private static final String TABLE_FIELD = "TANAMAN_BORDIR, TANAMAN_LATAR, TANAMAN_PELINDUNG, TANAMAN_PEMANIS";
public SQLiteDatabase getDb() {
return db;
}
public DBHelper (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
createDb();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDb() {
boolean dbExist = checkDbExist();
if (!dbExist) {
this.getReadableDatabase();
copyDatabase();
}
}
private boolean checkDbExist() {
SQLiteDatabase sqLiteDatabase = null;
try {
String path = DATABASE_PATH + DATABASE_NAME;
sqLiteDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
} catch (Exception ex) {
}
if (sqLiteDatabase != null) {
sqLiteDatabase.close();
return true;
}
return false;
}
private void copyDatabase() {
try {
InputStream inputStream = context.getAssets().open(DATABASE_NAME);
String outFileName = DATABASE_PATH + DATABASE_NAME;
OutputStream outputStream = new FileOutputStream(outFileName);
byte[] b = new byte[1024];
int length;
while ((length = inputStream.read(b)) > 0) {
outputStream.write(b, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private SQLiteDatabase openDatabase() {
String path = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
return db;
}
public void close() {
if (db != null) {
db.close();
}
}
public boolean checkUserExist(String username, String password) {
String[] columns = {"username"};
db = openDatabase();
String selection = "username=? and password = ?";
String[] selectionArgs = {username, password};
Cursor cursor = db.query(USER_TABLE, columns, selection, selectionArgs, null, null, null);
int count = cursor.getCount();
cursor.close();
close();
if (count > 0) {
return true;
} else {
return false;
}
}
}
这是LoginActivity.java
类:
public class LoginActivity extends AppCompatActivity {
Button btnLogin;
EditText edtUsername;
EditText edtPassword;
DBHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin = (Button) findViewById(R.id.btn_login);
edtUsername = (EditText) findViewById(R.id.edittextuname);
edtPassword = (EditText) findViewById(R.id.edittextpassword);
databaseHelper = new DBHelper (LoginActivity.this);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isExist = databaseHelper.checkUserExist(edtUsername.getText().toString(), edtPassword.getText().toString());
if(isExist){
Intent masuk = new Intent(LoginActivity.this, makeNote.class);
masuk.putExtra("username", edtUsername.getText().toString());
startActivity(masuk);
} else {
edtPassword.setText(null);
Toast.makeText(LoginActivity.this, "Gagal Masuk!\n\nUsername / Password salah", Toast.LENGTH_SHORT).show();
}
}
});
}
}
并且我想在makeNote.java
类上实现它:
package com.abs.danisflora.nota;
public class makeNote extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_makeNote);
}
}