此处的用户界面
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();
}
}
});
}
//从表帐户中的DataBase中检索数据 public 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 account){
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();
}
答案 0 :(得分:0)
首先,您的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();
}
}