【问题标题】:Sqlite Database and Radio Buttons in Android StudioAndroid Studio 中的 Sqlite 数据库和单选按钮
【发布时间】:2016-04-13 18:19:22
【问题描述】:

用户界面
User interface
和数据库
Sqlite Database
在我的代码中,我想比较选定的单选按钮是否等于数据库中的帐户类型。
如果它们相等,则必须按照 if 语句中的说明开始活动。 我对我的帐户类型进行了硬编码,它可以工作,但现在我想从数据库中获取帐户类型并将其与所选单选按钮的字符串进行比较

 //Button To submit selected Radio button for a specicific account
    public void onClickSubmitAccount(){
        radio_accounts = (RadioGroup) findViewById(R.id.radioAccounts);
        btnSubmitAccount = (Button) findViewById(R.id.btnSubmitAccount);

        btnSubmitAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                int selected_account = radio_accounts.getCheckedRadioButtonId();
                radio_button_accounts = (RadioButton) findViewById(selected_account);
                Toast.makeText(UserHomePageActivity.this, radio_button_accounts.getText().toString(), Toast.LENGTH_SHORT).show();



                if ((radio_button_accounts.getText().toString()).equals("Savings Account")) {
                    Intent accountIntent = new Intent(UserHomePageActivity.this, SavingsTransactionsActivity.class);
                    startActivity(accountIntent);
                }

                else if ((radio_button_accounts.getText().toString()).equals("Credit     Account")) {
                    Intent accountIntent = new Intent(UserHomePageActivity.this, CreditTransactionActivity.class);
                    startActivity(accountIntent);
                }

                else if ((radio_button_accounts.getText().toString()).equals("Cheque  Account")) {
                    Intent accountIntent = new Intent(UserHomePageActivity.this, ChequeTransactionActivity.class);
                    startActivity(accountIntent);
                }

                else {
                    Toast.makeText(UserHomePageActivity.this, "Sorry, No accout has been selected", Toast.LENGTH_LONG).show();
                }



            }
        });
    }

//从表账户的数据库中获取数据 公共 ArrayList getAllAccount() {

    ArrayList<AccountClass>   accountList = new ArrayList<AccountClass>();
    Cursor cursor = bankingAppDB.query(Constants.tblAccount, null,null,null,null,null,null);
    AccountClass account;

    if(cursor.moveToFirst()){

        while (cursor.moveToNext())
        {
            String acc_number = cursor.getString(cursor.getColumnIndex(Constants.ACCOUNT_NUMBER));
            String account_type = cursor.getString(cursor.getColumnIndex(Constants.ACCOUNT_TYPE));
            double balance = Double.parseDouble(cursor.getString(cursor.getColumnIndex(Constants.ACCOUNT_BALANCE)));

            Log.d(" Info"," Account Number: " +acc_number+" type: "+account_type);
            account = new AccountClass(acc_number);
            accountList.add(account);
        }
    }
    return accountList;
}

//在账户表中添加账号、账户类型和余额 public void addAccount(AccountClass 帐户){

    ContentValues values = new ContentValues();
    values.put(Constants.ACCOUNT_NUMBER, account.getAccountNumber());
    values.put(Constants.ACCOUNT_BALANCE, account.getAccountBalance());
    values.put(Constants.ACCOUNT_TYPE, account.getAccountType());
    bankingAppDB.insert(Constants.tblAccount, null, values);
    bankingAppDB.close();

}

【问题讨论】:

  • 您的数据库处理程序在哪里?
  • 我正在尝试复制数据库处理程序的代码,它太长了,请帮助我如何上传课程@Ricardo Barroca
  • 你没有getAccount或getAccountType的方法吗?
  • 我重新编辑了我的代码,希望现在有意义

标签: android sqlite


【解决方案1】:

首先,您的 AccountClass 仅接收帐号属性,因为您没有将其余值存储在对象中。

更改您的构造函数以获取所有 3 个属性

 account = new AccountClass(acc_number, account_type, balance);

其次,你应该有一个方法,通过它的号码来获取一个帐户

 public List<AccountClass> getAccountsByNumber(String number){
      ArrayList<AccountClass>   accountList = new ArrayList<AccountClass>();
      String query = "SELECT * FROM " + YOUR_TABLE + " WHERE " + ACCOUNT_NUMBER + " = " + number;
      Cursor cursor = bankingAppDB.query(query, null,null,null,null,null,null);

      if(cursor.moveToFirst()){

    while (cursor.moveToNext())
    {
        String acc_number = cursor.getString(cursor.getColumnIndex(Constants.ACCOUNT_NUMBER));
        String account_type = cursor.getString(cursor.getColumnIndex(Constants.ACCOUNT_TYPE));
        double balance = Double.parseDouble(cursor.getString(cursor.getColumnIndex(Constants.ACCOUNT_BALANCE)));

        Log.d(" Info"," Account Number: " +acc_number+" type: "+account_type);
        AcountClass account = new AccountClass(number,account_type,balance);
        accountList.add(account);
    }
}
      return accountList;
 }

之后,假设您知道您的用户 ID,在 onClick 方法调用中

List<AccountClass> accountList = bankingAppDB.getAccountsByNumber(NUMBER_OF_THE_ACCOUNT);

然后

for(AccountClass a : accountList){
if ((radio_button_accounts.getText().toString()).equals(a.getAccountType()) && a.getAccountType().trim().equals("Savings Account")) {
                Intent accountIntent = new Intent(UserHomePageActivity.this, SavingsTransactionsActivity.class);
                startActivity(accountIntent);
            }

            else if ((radio_button_accounts.getText().toString()).equals(a.getAccountType())&& a.getAccountType().trim().equals("Credit Account")) {
                Intent accountIntent = new Intent(UserHomePageActivity.this, CreditTransactionActivity.class);
                startActivity(accountIntent);
            }

            else if ((radio_button_accounts.getText().toString()).equals(a.getAccountType())&& a.getAccountType().trim().equals("Cheque Account")) {
                Intent accountIntent = new Intent(UserHomePageActivity.this, ChequeTransactionActivity.class);
                startActivity(accountIntent);
            }

            else {
                Toast.makeText(UserHomePageActivity.this, "Sorry, No accout has been selected", Toast.LENGTH_LONG).show();
            }
}

【讨论】:

  • 非常感谢@Ricardo
猜你喜欢
  • 2021-12-06
  • 1970-01-01
  • 2015-03-03
  • 2017-03-28
  • 2014-04-26
  • 1970-01-01
  • 2017-09-27
  • 2012-04-21
  • 1970-01-01
相关资源
最近更新 更多