现有SQLite数据库中的微调器

时间:2018-08-06 03:40:59

标签: android sqlite android-sqlite

我是业余爱好者,需要帮助。

我想使用现有的SQLite数据库制作Spinner,例如:my spinner layout

  • 第一个微调器从1个表中出现4个字段。
  • 如果我单击一个字段,则所选字段的记录将显示在第二个微调器中。
  • 第二个微调器右侧的文本编辑,用于插入我选择的记录的数量。
  • 单击“ Tambah”按钮后,带有金额的记录将保存在下面的列表视图中。
  • 按钮“ Simpan”,用于将所有进度保存到新的Intent。

我已成功在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);
    }
}

0 个答案:

没有答案