【问题标题】:how to set a specific value from the array for a spinner based on another spinner?如何从数组中为基于另一个微调器的微调器设置特定值?
【发布时间】:2016-05-25 01:01:40
【问题描述】:

在我的应用程序中,我有两个微调器(职业、子职业),其中第一个微调器是从字符串数组中填充的,第二个微调器是根据第一个微调器中选择的值填充的。

两个微调器的选定值都存储在 SQLite 数据库中。保存后用户可以编辑记录,因此在显示要编辑的记录时,我想在用户上次选择的微调器上显示特定值。

当我尝试这样做时,第一个微调器值设置正确,但我无法设置第二个微调器值。它始终显示该微调器的数组中的第一个值。

这是在编辑页面中为微调器分配值的代码:

    if (bundlevalue.get(21).equalsIgnoreCase("Salaried")) {
        spin_occupation.setSelection(0);
        if(bundlevalue.get(22).equalsIgnoreCase("Others"))
        {
            spin_subOccu.setSelection(4);
            occuSubArrayAdap.notifyDataSetChanged();
        }
        else if(bundlevalue.get(22).equalsIgnoreCase("Police"))
        {
            spin_subOccu.setSelection(1);
            occuSubArrayAdap.notifyDataSetChanged();
        }
        else if(bundlevalue.get(22).equalsIgnoreCase("Legal Profession"))
        {
            spin_subOccu.setSelection(2);
            occuSubArrayAdap.notifyDataSetChanged();
        }
        else if(bundlevalue.get(22).equalsIgnoreCase("Central/State Government"))
        {
            spin_subOccu.setSelection(3);
            occuSubArrayAdap.notifyDataSetChanged();
        }
        else
        {
            spin_subOccu.setSelection(0);
            occuSubArrayAdap.notifyDataSetChanged();
        }
    }
     else if (bundlevalue.get(21).equalsIgnoreCase(
            "Self employed non professional")) {
        spin_occupation.setSelection(1);
        if(bundlevalue.get(22).equalsIgnoreCase("Others"))
        {
            spin_subOccu.setSelection(5);
            occuSubArrayAdap.notifyDataSetChanged();
        }
        else if(bundlevalue.get(22).equalsIgnoreCase("Travel Agent /Telecommunication Service/Tours&Travels"))
        {
            spin_subOccu.setSelection(1);
            occuSubArrayAdap.notifyDataSetChanged();
        }
        else if(bundlevalue.get(22).equalsIgnoreCase("Restaurant/Hotels/Resorts"))
        {
            spin_subOccu.setSelection(2);
            occuSubArrayAdap.notifyDataSetChanged();
        }
        else if(bundlevalue.get(22).equalsIgnoreCase("Retail Stores"))
        {
            spin_subOccu.setSelection(3);
            occuSubArrayAdap.notifyDataSetChanged();
        }
        else if(bundlevalue.get(22).equalsIgnoreCase("Money Changers/Money Lenders/Real Estate"))
        {
            spin_subOccu.setSelection(4);
            occuSubArrayAdap.notifyDataSetChanged();
        }
        else
        {
            spin_subOccu.setSelection(0);
            occuSubArrayAdap.notifyDataSetChanged();
        }

我的代码有什么问题?有人可以解释一下吗?

请帮忙!

提前致谢!

【问题讨论】:

  • 您的 sub_spinner 是否有多个值要显示?
  • @r4jiv007 是的!该值取决于第一个微调器..

标签: android android-spinner


【解决方案1】:

将 OnItemClickListener() 设置为您的 super_spinner,并在选择任何项目时尝试更改您传递给 sub_spinner 的 Adapter 的值。

super_spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // change adapter values of sub_spinner here !!
        }
    });

【讨论】:

  • 是的,我正在以与您展示的方式相同的方式进行操作......但我想在微调器上显示保存在数据库中的唯一值,就像我在代码中显示的那样...... .我根据第一个微调器值得到正确的数组,但我需要在微调器上显示该数组的正确值.....这没有发生......
【解决方案2】:

更新第一个微调器 setOnItemSelectedListener 中第二个微调器的数组列表

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub


                string str=spinner1.getSelectedItem().toString();
                if(str.equals("spinner item"))//spinner item is selected item of spinner1
                {
                    ArrayAdapter<String>adapter1 = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, array1);

             spinner2.setAdapter(adapter1);//pass this updated adapter1 to second spinner
                }else if{
               ArrayAdapter<String>adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array2);

             spinner2.setAdapter(adapter2);

    }
        }

【讨论】:

  • 是的,我正在以与您展示的方式相同的方式进行操作......但我想在微调器上显示保存在数据库中的唯一值,就像我在代码中显示的那样...... .我根据第一个微调器值得到正确的数组,但我需要在微调器上显示该数组的正确值.....这没有发生......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
  • 1970-01-01
  • 2016-01-22
  • 2016-08-17
  • 1970-01-01
相关资源
最近更新 更多