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 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题