如何将微调器数据从一项活动传递到另一项活动? [英] How to pass spinner data from one activity to another ?

查看:97
本文介绍了如何将微调器数据从一项活动传递到另一项活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码未从微调器中读取值,而是始终仅读取第一个值,

This code is not reading the value from the spinner it's reading only the first value always,

btnResult.setOnClickListener(new View.OnClickListener() 
{
    final String USN = spnConversions.getSelectedItem().toString();
    @Override
    public void onClick(View v) 
    {
        Intent i = new Intent(getApplicationContext(), DatabaseResult.class);
        i.putExtra("getData",USN.toString());
        startActivity(i);
    }
});

推荐答案

为什么要在Spinner上使用onClickListener?您应该对Spinner使用OnItemSelectedListener(),请参见下面的示例代码

Why you are using onClickListener for Spinner ? You should use OnItemSelectedListener() for Spinner see the below example code,

public class MySpinnerSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String selected = parent.getItemAtPosition(pos).toString();
    }

    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}

现在使用以下代码注册侦听器

Now register listener using following code,

 spinner.setOnItemSelectedListener(new MySpinnerSelectedListener());

您可以使用以下代码进行传递,

You can pass it using following code,

//正在发送代码

Intent intent = new Intent(getApplicationContext(), DatabaseResult.class);
intent.putextra("getData",USN.toString());
startActivity(intent);

//接收代码

String value= getIntent().getStringExtra("getData");

这篇关于如何将微调器数据从一项活动传递到另一项活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆