将notifyDataSetChanged与BaseAdapter一起使用

时间:2018-08-22 02:03:58

标签: java android baseadapter

更改第一个微调器时,我正在尝试更改第二个微调器。这就是我尝试过的。

spinnerBid = findViewById(R.id.profile_spinner_bid);
    spinnerBid.setAdapter(null);
    unitBAdapter = new UnitBAdapter(ProfileActivity.this, getUnitBs(aId));
    spinnerBid.setAdapter(unitBAdapter);
    int positionB = -1;
    for (int i = 0; i < getUnitBs(aId).size(); i++) {
        if (getUnitBs(aId).get(i).getBId() == bId) {
            positionB = i;
        }
    }
    spinnerBid.setSelection(positionB);
    unitBAdapter.notifyDataSetChanged(); 

1 个答案:

答案 0 :(得分:0)

您可以根据需要在onCreate()中执行以下代码,

        spinner1 = findViewById(R.id.spinner1);
        spinner2 = findViewById(R.id.spinner2);

        String[] s1 = {"abc", "def", "ghi", "jkl"};
        String[] s2 = {"mno", "pqr", "stu", "vwx"};

        ArrayAdapter<String> adapter1 =new  ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, s1);
        ArrayAdapter<String> adapter2 =new  ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, s2);

        spinner1.setAdapter(adapter1);
        spinner2.setAdapter(adapter2);

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //here you can write your code for your requirements like,
                // if in spinner1 position 1 is selected then in spinner2 set position to 1 or anything you want
                if(position==0){
                    spinner2.setSelection(0);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

我希望这对您有帮助。