javascript - 将选定的值传递给另一个 Android 类

标签 javascript android arrays android-intent android-spinner

我试图将从微调器中选择的值传递给另一个类,但它传递了 null。

protected int secondsToUse;

final String titles[] = {
  "1 Second","2 Seconds", "3 Seconds","4 Seconds",
  "5 Seconds","6 Seconds","7 Seconds","8 Seconds","9 Seconds","10 Seconds"
};

mSeconds = (Spinner) view.findViewById(R.id.secondsSpinner);

 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, titles);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mSeconds.setAdapter(arrayAdapter);
mSeconds.setOnItemSelectedListener(
    new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // secondsToUse is declared a as int variable above
            secondsToUse = position + 1;
        }



// passing the value to another class.// PASSES AS NULL
Intent recipientsIntent = new Intent(getActivity(), RecipientsActivity.class);
                    recipientsIntent.putExtra("key1", secondsToUse);
                    startActivity(recipientsIntent);

// retrieving the value //retrieves as null
String Seconds = getIntent().getStringExtra("key1");
byte[] secs = Seconds.getBytes();

我正在尝试获取用户在旋转器上选择的位置,以便我可以将其传递到计时器中,以便图像自毁。此刻我得到空

最佳答案

编辑:我忘记考虑到 getIntExtra() 在我的第一个答案中采用两个参数。它需要一个识别“键”和一个默认值。

要检索 RecipientsActivity 中所选行的位置,请保持代码不变。然后,在RecipientsActivityonCreate()中:

int seconds = getIntent().getIntExtra("key1", -1);
<小时/>

关于javascript - 将选定的值传递给另一个 Android 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32155720/

相关文章:

android - 哪个更好?原生应用还是混合应用?

arrays - tcl 数组问题 - 带引号的键

在 C 语言的替换程序中成功进行初始输入后,无法输入第二个输入

javascript - 在 onchange 事件上添加变量值

javascript - 从集合中更新 View

javascript - Google Analytics 不跟踪交易

iphone - iPhone/Android 浏览器上的 Google Maps API InfoWindow 高度不正确

android - 根据 gps 状态 android 可互换的网络和 GPS 位置提供者

c++ - 使用构造函数初始化成员数组

javascript - React 不将 HTML 字符串从 JSON 转换为文本