猿问

选择 spinner 中的合适的值

我在创建一个 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 数组的第一项?然后我就可以相应的编辑和改变它。


慕桂英4014372
浏览 571回答 2
2回答

精慕HU

使用这个方法:String&nbsp;yourAccountTypeInString; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(myAccount.accountType&nbsp;==&nbsp;yourAccountType) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yourAccountTypeInString=&nbsp;"theStringOfAccountType"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;use&nbsp;else&nbsp;if&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;pos&nbsp;=&nbsp;typeOfAccountAdapter.getPosition(yourAccountTypeInString); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typeOfAccount.setSelection(pos);
随时随地看视频慕课网APP

相关分类

Java
我要回答