【问题标题】:Passing an array item to another activity, from a spinner从微调器将数组项传递给另一个活动
【发布时间】:2012-09-26 23:55:07
【问题描述】:

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

关于活动:

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>

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

【问题讨论】:

    标签: android arrays sdk resources spinner


    【解决方案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");
    }
    

    【讨论】:

    • 哪些是?在不知道什么的情况下无法对这些信息做太多事情
    • 修复了一个错误...您需要自己更改ClassB。你打算上什么课?
    • 将整个意图部分移动到一个新的方法,如启动活动。我会尽快发布代码。
    • 而不仅仅是this&lt;activity name&gt;.this
    【解决方案2】:
     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);
        }
    

    【讨论】:

    • 文本视图在哪里?它是在同一个活动中还是在另一个活动中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多