根据我的班级选择适当的微调器值

时间:2012-03-07 05:35:05

标签: java android

我正在制作我的帐户的编辑屏幕。

我的帐户类有一些属性。现在我想显示这些属性,然后编辑它们。 我制作了一个显示帐户类型的微调器。

现在我正在使用此代码

 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

我如何才能显示typeOfAccountArray的{​​{1}}数组的第一项,accountType的{​​{1}}

然后我可以相应地编辑和更改它。

2 个答案:

答案 0 :(得分:0)

String yourAccountTypeInString;
               if (myAccount.accountType == yourAccountType)
                   yourAccountTypeInString= "theStringOfAccountType";
         // use else if 

               int pos = typeOfAccountAdapter.getPosition(yourAccountTypeInString);
               typeOfAccount.setSelection(pos);

试试这个并告诉

答案 1 :(得分:0)

 typeOfAccount.setOnItemSelectedListener(new OnItemSelectedListener() 
             {
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
                {  
       constants_class.GROUP=(String) typeOfAccountAdapter.getItem(arg2).toString();
        if(constants_class.GROUP.equalsIgnoreCase("Income")){

        }
        else if(constants_class.GROUP.equalsIgnoreCase("Asset")){

        }  
       else if(constants_class.GROUP.equalsIgnoreCase("Asset")){

        }
}});  

默认微调器取第一个值,但是当你点击微调器并选择任何一个项时它会正确。希望这对你有帮助