第二个微调器不更新值

时间:2017-11-15 08:48:25

标签: java android android-spinner

如果我更改或添加第一个微调器(spinnerBreeds)值 - 工作正常。但是第二个spinner不会更新值。我尝试拨打notifyDataSetChanged()上的adapter,但仍无效。

我的代码是:

    public class MainActivity extends Activity {

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

            // Create DataHelper object and insert some sample data
            DataHelper datahelper = new DataHelper(this);
            datahelper.insertBreed ("Beagle");
            datahelper.insertBreed ("Bulldog");
            datahelper.insertBreed ("Chow Chow");

            // Get sample data from the database and display them in the spinner
            Spinner spinnerBreeds = (Spinner) findViewById(R.id.spinnerBreeds);
            ArrayList<String> list = datahelper.getAllBreeds();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, list);
            spinnerBreeds.setAdapter(adapter);



            //Init second spinner
            DataHelper2 datahelper2 = new DataHelper2(this);
            datahelper2.insertAge("1 year");
            datahelper2.insertAge ("2-3 years");
            datahelper2.insertAge("4-5 years");
            datahelper2.insertAge("6-9 years");
            datahelper2.insertAge("10+ years");


            // Get sample data from the database and display them in the spinner
            Spinner spinnerAge = (Spinner) findViewById(R.id.spinnerAge);
            ArrayList<String> list2 = datahelper2.getAllAge();
            ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, list2);
            spinnerAge.setAdapter(adapter2);






    }

2 个答案:

答案 0 :(得分:1)

Try this code........   
spinnerBreeds.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view,
                            int position, long id) {
                        // TODO Auto-generated method stub

 ArrayList<String> list2 = datahelper2.getAllAge();
  ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, list2);

                spinnerAge.setAdapter(adapter2);
                    }

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

                    }
                });

答案 1 :(得分:0)

替换这些:

new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, list);
new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, list2)

使用:

new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, list);
new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, list2)

而不是这个:

ArrayList<String> list = datahelper.getAllBreeds();

使用:

ArrayList<String> list = new ArrayList<String>();
for(DataHelper d: dataHelper)
        {
            if (d.getBreed() != null)
            {
                list1.add(d.getBreed();
            }
        }

而不是:

ArrayList<String> list2 = datahelper2.getAllAge();

使用:

ArrayList<String> list2 = new ArrayList<String>();
for(DataHelper2 a : dataHelper2)
        {
            if (a.getAge() != null)
            {
                list2.add(a.getAge();
            }
        }