spinner数据依赖于android

时间:2013-08-22 14:26:42

标签: android arrays spinner

遇到问题

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

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

为具有主数组的第一个微调器创建第一个适配器。 并且依赖于项目选择必须克服具有依赖于选择的阵列的适配器 如果是第1项,那么适配器包含数组1,如果第2项则是数组2

3 个答案:

答案 0 :(得分:1)

您将创建spinner,并根据第一个Adapterspinner的选择,为第二个onItemSelected设置Spinner。像

这样的东西
@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

答案 1 :(得分: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;
    }
}

答案 2 :(得分: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

    }

}

}

我希望这会帮助你。 :)