我在創建一個 Account 的 edit Screen。
Account class 有一些屬性,現在我想顯示這些屬性然後編輯它們。我創建了一個spinner 然後顯示account type。
ArrayAdapter<CharSequence> typeOfAccountAdapter = ArrayAdapter.createFromResource(
this, R.array.typeOfAccountArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
typeOfAccount.setAdapter(typeOfAccountAdapter);
typeOfAccount.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
if (typeOfAccount.getSelectedItem().toString().equals("Income"))
myAccount.accountType = AccountType.kAccountTypeIncome;
else if(typeOfAccount.getSelectedItem().toString().equals("Asset"))
myAccount.accountType = AccountType.kAccountTypeAsset;
else if(typeOfAccount.getSelectedItem().toString().equals("Cash"))
myAccount.accountType = AccountType.kAccountTypeAssetCash;
else if(typeOfAccount.getSelectedItem().toString().equals("Bank"))
myAccount.accountType = AccountType.kAccountTypeAssetBank;
else if(typeOfAccount.getSelectedItem().toString().equals("Liability"))
myAccount.accountType = AccountType.kAccountTypeLiability;
else
myAccount.accountType = AccountType.kAccountTypeLiabilityOther;
setStrDeatilOfAccount();
}
這段代碼實際上不是顯示 myAccount.accountType
,設置spinner的第一個元素為accountType。
如何顯示 myAccount 的 accountType。而不是 typeOfAccountArray 數組的第一項?然後我就可以相應的編輯和改變它。
使用這個方法:
String yourAccountTypeInString;
if (myAccount.accountType == yourAccountType)
yourAccountTypeInString= "theStringOfAccountType";
// use else if
int pos = typeOfAccountAdapter.getPosition(yourAccountTypeInString);
typeOfAccount.setSelection(pos);