【问题标题】:Populating multiple dependent spinners via string resource通过字符串资源填充多个依赖微调器
【发布时间】:2018-07-30 02:19:41
【问题描述】:

作为一个苦苦挣扎的新手,我设置了 3 个微调器,它们根据选择的项目来拉字符串数组。这是一个年份、品牌和型号设置,用于对车辆进行分类,根据所选车辆从 sqlite3 数据库中提取信息。所以 Year 是一个基本的微调器(2000 年到 2019 年)。但是,下一个微调器 Make 会根据所选年份进行填充。此外,模型微调器取决于选择的 Make。我有大约。 80 个字符串数组,用于仅在特定年份可用的模型。说了这么多,我已经从研究中拼凑出这个科学怪人的怪物代码。我能想到的唯一解决方案是大约 100 个 if 语句,以使这些微调器填充所有选项。以下是已被删减的代码,以防止重复发生时浪费空间。 非常感谢任何批评、帮助或想法。

MainActivity.java

 import android.support.annotation.NonNull;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.Toolbar;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;

    public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener,
                                                                    AdapterView.OnItemSelectedListener {

        private  DrawerLayout drawer;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            spinStrYear = getResources().getStringArray(R.array.year);
            spinStrMake = getResources().getStringArray(R.array.make);
            spinStrModel = getResources().getStringArray(R.array.model);
            spinYear = findViewById(R.id.spinnerYear);
            spinMake = findViewById(R.id.spinnerMake);
            spinModel = findViewById(R.id.spinnerModel);

            ar = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinStrYear);
            ar2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinStrMake);
            ar3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinStrModel);
            spinYear.setAdapter(ar);
            spinMake.setAdapter(ar2);
            spinModel.setAdapter(ar3);

            spinYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View view,
                                       int position, long id) {

                if (spinYear.getSelectedItem().equals("2000"))
                    ar.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spinMake.setAdapter(ar2);
            }


                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub
                }
            });

            spinMake.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View view,
                                       int position, long id) {
if (spinMake.getSelectedItem().equals("Acura")) {
                    ar2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spinModel.setAdapter(ar3);
                }

                if (spinMake.getSelectedItem().equals("Audi")) {

                }

                if (spinMake.getSelectedItem().equals("BMW"))  {

                }

                if (spinMake.getSelectedItem().equals("Cadillac"))  {

                }

Strings.xml

<resources>
    <string-array name="year">   // Year spinner population
        <item>2000</item>
        <item>2001</item>
        <item>2002</item>
        <item>2003</item>
        <item>2004</item>
        <item>2005</item>
        <item>2006</item>
        <item>2007</item>
        <item>2008</item>
        <item>2009</item>
        <item>2010</item>
        <item>2011</item>
        <item>2012</item>
        <item>2013</item>
        <item>2014</item>
        <item>2015</item>
        <item>2016</item>
        <item>2017</item>
        <item>2018</item>
        <item>2019</item>
    </string-array>                 // End of year spinner population

    <string-array name="make">       // Make spinner population
    <item>Acura</item>
    <item>Audi</item>
    <item>BMW</item>
    <item>Buick</item>
    <item>Cadillac</item>
    </string-array>                     // End of Make spinner population


    <string-array name="Acura2000">       // Model spinner population
        <item>Integra</item>
        <item>NSX</item>
        <item>RL</item>
        <item>TL</item>
    </string-array>

    <string-array name="Acura2001">
        <item>CL</item>
        <item>Integra</item>
        <item>MDX</item>
        <item>NSX</item>
        <item>RL</item>
        <item>TL</item>
    </string-array>

    <string-array name="Acura2002_03"> // Same data for 02 and 03
        <item>CL</item>
        <item>MDX</item>
        <item>NSX</item>
        <item>RL</item>
        <item>RSX</item>
        <item>TL</item>
    </string-array>

    <string-array name="Acura2004_05"> // Same data for 04 and 05
        <item>MDX</item>
        <item>NSX</item>
        <item>RL</item>
        <item>RSX</item>
        <item>TL</item>
        <item>TSX</item>
    </string-array>

    <string-array name="Acura2006">
    <item>MDX</item>
    <item>RL</item>
    <item>RSX</item>
    <item>TL</item>
    <item>TSX</item>
</string-array>

【问题讨论】:

    标签: java android spinner android-spinner


    【解决方案1】:

    而不是在每个 Year 中为每个 Make 制作字符串数组。我重新安排了 MakeModel 及其生产周期,然后在运行时生成列表。请尝试以下操作:

    MainActivity.java:

    public class MainActivity extends AppCompatActivity {
    Spinner spinYear, spinMake, spinModel;
    ArrayAdapter ar, ar2, ar3;
    String[] spinStrYear;
    ArrayList<String> spinStrMake, spinStrModel;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        spinYear = findViewById(R.id.spinner1);
        spinMake = findViewById(R.id.spinner2);
        spinModel = findViewById(R.id.spinner3);
    
        spinStrYear = getResources().getStringArray(R.array.year);
        spinStrMake = new ArrayList<>();
        spinStrModel = new ArrayList<>();
    
        ar = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinStrYear);
        ar2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinStrMake);
        ar3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinStrModel);
        spinYear.setAdapter(ar);
        spinMake.setAdapter(ar2);
        spinModel.setAdapter(ar3);
    
        spinYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
                updateSpinnerMake();
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
        spinMake.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
                updateSpinnerModel(position);
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
    }
    
    private void updateSpinnerMake() {
        int selectedYear = Integer.parseInt(spinYear.getSelectedItem().toString());
        spinStrMake.clear();
        String[] tmpMakeArray = getResources().getStringArray(R.array.make);
        for (String tmpMake : tmpMakeArray) {
            String[] tmpMakeDetails = getResources().getStringArray(getResources().getIdentifier(
                    tmpMake, "array", getPackageName()));
            int startYear = Integer.parseInt(tmpMakeDetails[0]);
            int stopYear = Integer.parseInt(tmpMakeDetails[1]);
            if (selectedYear >= startYear && selectedYear <= stopYear) spinStrMake.add(tmpMake);
        }
        if (spinStrMake.size() == 0) spinStrMake.add("No make on that year!!");
        ar2.notifyDataSetChanged();
    
        if (spinMake.getSelectedItemPosition() >= 0) updateSpinnerModel(spinMake.getSelectedItemPosition());
        else updateSpinnerModel(0);
    }
    
    private void updateSpinnerModel(int position) {
        spinStrModel.clear();
        if (!spinStrMake.get(0).equals("No make on that year!!")){
            int selectedYear = Integer.parseInt(spinYear.getSelectedItem().toString());
            String[] tmpModelArray = getResources().getStringArray(getResources().getIdentifier(
                    spinStrMake.get(position), "array", getPackageName()));
            String tmpModel;
            for (int j=2; j<tmpModelArray.length; j++) {
                tmpModel = tmpModelArray[j];
                String[] period = getResources().getStringArray(getResources().getIdentifier(
                        tmpModel, "array", getPackageName()));
                int startYear = Integer.parseInt(period[0]);
                int stopYear = Integer.parseInt(period[1]);
                if (selectedYear >= startYear && selectedYear <= stopYear)
                    spinStrModel.add(tmpModel);
            }
            if (spinStrModel.size() == 0) spinStrModel.add("No Match Model Found!!");
        }
        ar3.notifyDataSetChanged();
    }
    }
    

    strings.xml:

        <string-array name="year">// Year spinner population
        <item>2000</item>
        <item>2001</item>
        <item>2002</item>
        <item>2003</item>
        <item>2004</item>
        <item>2005</item>
        <item>2006</item>
        <item>2007</item>
        <item>2008</item>
        <item>2009</item>
        <item>2010</item>
        <item>2011</item>
        <item>2012</item>
        <item>2013</item>
        <item>2014</item>
        <item>2015</item>
        <item>2016</item>
        <item>2017</item>
        <item>2018</item>
        <item>2019</item>
    </string-array>// End of year spinner population
    
    <string-array name="make">// Make spinner population
        <item>Acura</item>
        <item>Audi</item>
    </string-array>// End of Make spinner population
    
    <string-array name="Acura">
        <item>2000</item>
        <item>2006</item>
        <item>CL</item>// All Models grouped under Acura should be here
        <item>Integra</item>
        <item>MDX</item>
        <item>NSX</item>
        <item>RL</item>
        <item>RSX</item>
        <item>TL</item>
        <item>TSX</item>
    </string-array>
    
    <string-array name="CL">
        <item>2001</item>
        <item>2003</item>
    </string-array>
    
    <string-array name="Integra">
        <item>2000</item>
        <item>2001</item>
    </string-array>
    
    <string-array name="MDX">
        <item>2001</item>
        <item>2006</item>
    </string-array>
    
    <string-array name="NSX">
        <item>2000</item>
        <item>2005</item>
    </string-array>
    
    <string-array name="RL">
        <item>2000</item>
        <item>2006</item>
    </string-array>
    
    <string-array name="RSX">
        <item>2002</item>
        <item>2006</item>
    </string-array>
    
    <string-array name="TL">
        <item>2000</item>
        <item>2006</item>
    </string-array>
    
    <string-array name="TSX">
        <item>2004</item>
        <item>2006</item>
    </string-array>
    
    <string-array name="Audi">
        <item>2001</item>
        <item>2007</item>
        <item>Audi1</item>
        <item>Audi2</item>
    </string-array>
    
    <string-array name="Audi1">
        <item>2001</item>
        <item>2006</item>
    </string-array>
    
    <string-array name="Audi2">
        <item>2004</item>
        <item>2007</item>
    </string-array>
    

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      相关资源
      最近更新 更多