从第一个微调器设置第二个微调器适配器

时间:2015-10-23 08:11:49

标签: android arrays spinner android-arrayadapter adapter

我有两个微调器,我想在选择微调器1中的某个项时更改第二个微调器的数组适配器。 我有适配器工作,但当我在第二个微调器中选择项目时,所选的第二个微调器项目不会改变。

ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,R.array.weapons, android.R.layout.simple_spinner_item);
    s1.setAdapter(adapter1);
    s1.setOnItemSelectedListener(this);
    s2.setOnItemSelectedListener(this);

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    switch (parent.getId()) {
        case R.id.rspin:
            wepone = s1.getSelectedItem();
            break;
        case R.id.rspin2:
            n = s2.getSelectedItemPosition();
            com.setVisibility(View.GONE);
            break;
    }

    String[] weaponone = getResources().getStringArray(R.array.weapons);
    m = Arrays.asList(weaponone).indexOf(wepone);
    switch (m) {
        case (3):
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(release.this,R.array.weapons, android.R.layout.simple_spinner_item);
    s2.setAdapter(adapter2);
switch(n){
case(1):
ft.replace(R.id.all , combofrag, "p42");
ft.commit();
break;
}
break;
}

1 个答案:

答案 0 :(得分:0)

试试此代码

    Spinner sp1,sp2;

    String num[] = {"1","2","3","4","5"};
    String nums[] = {"1","2","3","4","5","6","7","8"};


        sp1 = (Spinner) findViewById(R.id.sp1);
        sp2 = (Spinner) findViewById(R.id.sp2);


        ArrayAdapter ad1=new ArrayAdapter(this,android.R.layout.simple_spinner_item,num);
        ad1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        sp1.setAdapter(ad1);

        sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

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


                long p = sp1.getItemIdAtPosition(arg2+1);
                Toast.makeText(getApplicationContext(), "Position : " +p, Toast.LENGTH_SHORT).show();

                if(p == 2){
                    callSpinner();
                }

            }

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

            }
        });


        sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

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

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

            }
        });

       protected void callSpinner() {
        // TODO Auto-generated method stub
        ArrayAdapter ad2=new ArrayAdapter(this, android.R.layout.simple_spinner_item,nums);
        ad2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        sp2.setAdapter(ad2);
    }

这对我来说很好......