从旋转器将数组项传递给另一个活动

时间:2012-09-26 23:55:08

标签: android arrays sdk resources spinner

大家好,请帮帮我,我有一个看起来像这样的微调器:

关于活动:

final Spinner cmbOpciones = (Spinner)findViewById(R.id.CmbOpciones);
spinner_adapter = ArrayAdapter.createFromResource( this, R.array.animal , android.R.layout.simple_spinner_item);
            spinner_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    cmbOpciones.setAdapter(spinner_adapter);

在布局上:

<Spinner  android:prompt="@string/Poronga"
                   android:layout_height="wrap_content" 
                   android:layout_width="fill_parent" 
                   android:id="@+id/CmbOpciones"/> 

使用数组:

<resources>
    <string-array name="animal">
        <item>Elephant</item>
        <item>Turtle</item>
        <item>Rabbit</item>
        <item>Mouse</item>
    </string-array>

</resources>

如何将微调器上数组的选定项传递给另一个活动,如textview或类似的东西?有没有办法用字符串做到这一点?感谢。

2 个答案:

答案 0 :(得分:1)

我会用这样的东西......

cmbOpciones.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int pos, long id) {
        Intent intent = new Intent(<YourActivity>.this, Horario.class);
        intent.putExtra("selected", parentView.getItemAtPosition(pos).toString());
        startActivity(intent);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }

});

然后在你打开的活动中......

Intent i = this.getIntent();
extras = i.getBundleExtra("extras");
String selected = "";
if(extras!=null){
    selected = extras.getStringExtra("selected");
}

答案 1 :(得分:0)

 cmbOpciones.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) 
            {
                Intent intent = new Intent(getApplicationContext(), Horario.class);
                intent.putExtra("selected", cmbOpciones.getSelectedItem().toString());
                startActivity(intent);

            }

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

            }
        });
    }

在接收方

  Bundle extras = getIntent().getExtras();
   if(extras != null)
    {
        String selectedItem = extras.getString("selected");
            TextView tv = (TextView)findViewById(R.id.txt01);
            tv.setText(selectedItem);
    }