0

有问题

在布局上有 2 个微调器,并希望获得此微调器结构的依赖

String[] main={"Moscov","LosAngeles","Pekin"};
String[] one={"1","2","3","4"};
String[] two={"2","2","5","8"};
String[] tree={"5","7","3","2"};

使用主阵列为第一个微调器创建第一个适配器。并且依赖于项目选择必须具有取决于选择的数组的 crete 适配器如果项目 1 则适配器与数组 1,如果项目 2 则数组 2

4

3 回答 3

1
@Override
public void onItemSelected(AdapterView<?> spinner, View view, int position,long arg3)
{

      switch(position) 
       { 
         case : 0;
       // called first array
        break;

       case : 1;
      // called second array
       break;
    }
}
于 2013-08-22T14:44:14.367 回答
1

您将根据在第一个上使用的选择创建spinner并设置Adapter第二个。就像是spinneronItemSelectedSpinner

@Override
public void onItemSelected(AdapterView<?> spinner, View view, int position,long arg3)
{
        TextView tv = (TextView)view;  // convert the view to TextView
        String selected = tv.getText().toString();
        if (selected.equals("Moscov"))
        {
            // set the Adapter here with the first set of values
        }
        else if(selected.equals("LosAngeles"))
        {
            // set the Adapter here with the second set of values
        }
        else
        {
            // set the Adapter here with the third set of values
        }
}

如果您将position您的文件String Arrays放入ArrayList.

于 2013-08-22T14:37:19.007 回答
0

签出此代码片段,在此微调器 2 的值取决于微调器 1

public class MainActivity extends Activity {

Spinner s1,s2;
Button btn;
String s;
TextView tv;
ArrayAdapter<String> adap1,adap2,adap3; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    s1=(Spinner)findViewById(R.id.spinner1);
    s2=(Spinner)findViewById(R.id.spinner2);
    btn=(Button)findViewById(R.id.button1);
    tv=(TextView)findViewById(R.id.tv1);


    String[] v1=getResources().getStringArray(R.array.c1);
    String[] v2=getResources().getStringArray(R.array.c2);
    String[] v3=getResources().getStringArray(R.array.c3);

    adap1=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v1);
    adap2=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v2);
    adap3=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v3);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public class ItemSelectedListenerr implements OnItemSelectedListener  {

    String[] v=getResources().getStringArray(R.array.c1);
    public String s;
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

        s=arg0.getItemAtPosition(arg2).toString();
        if(arg2==0)
        {
            s2.setAdapter(adap1);
        }
        if(arg2==1)
        {
            s2.setAdapter(adap2);
        }
        if(arg2==2)
        {
            s2.setAdapter(adap3);
        }

        tv.setText(s);
        Log.i("hahaha", "item selected is"+s);

    }


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

    }

}

}

我希望这会帮助你。:)

于 2013-08-22T14:44:22.300 回答