markdowneide 2013-05-29 09:53 采纳率: 0%
浏览 2362
已采纳

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

  • 写回答

2条回答

  • Baby_Bonnie 2013-05-30 01:45
    关注

    使用这个方法:

    String yourAccountTypeInString;
                   if (myAccount.accountType == yourAccountType)
                       yourAccountTypeInString= "theStringOfAccountType";
             // use else if 
    
                   int pos = typeOfAccountAdapter.getPosition(yourAccountTypeInString);
                   typeOfAccount.setSelection(pos);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛