简体   繁体   English

从微调器将数组项传递到另一个活动

[英]Passing an array item to another activity, from a spinner

Hi guys please help me here, I have a spinner that looks like this: 大家好,请在这里帮助我,我有一个旋转器,看起来像这样:

On the Activity: 关于活动:

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);

On the Layout: 在版面上:

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

With an array: 使用数组:

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

</resources>

How can I pass the selected item of the array on the spinner, to another activity, like in a textview or something like that? 如何将微调器上数组的选定项传递到另一个活动,例如在textview中或类似的东西? Is there a way to do this with strings? 有没有办法用字符串做到这一点? Thanks. 谢谢。

I would use something like this... 我会用这样的东西...

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
    }

});

Then in the activity that you open... 然后在您打开的活动中...

Intent i = this.getIntent();
extras = i.getBundleExtra("extras");
String selected = "";
if(extras!=null){
    selected = extras.getStringExtra("selected");
}
 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

            }
        });
    }

on Receiving side 在接收方

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM