将所选值传递给另一个Android类

时间:2015-08-22 11:43:41

标签: javascript android arrays android-intent android-spinner

我正在尝试将从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();

我正在尝试获取用户在微调器上选择的位置,因此我可以将其传递到计时器中以使图像自毁。目前我得到了空白

5 个答案:

答案 0 :(得分:1)

修改:我忘了在第一个回答中考虑getIntExtra()采用两个参数。它需要识别&#34;键&#34;和默认值。

要在RecipientsActivity中检索所选行的位置,请保留您的代码。然后,在onCreate()的{​​{1}}中:

RecipientsActivity

答案 1 :(得分:0)

只需将getStringExtra()替换为getIntExtra()

int defaultSecond = -1;
int seconds = getIntent().getIntExtra("key1",defaultSecond);

原因:因为您在int

中放置了String而不是putExtra("key1", secondsToUse);

此外,在命名变量时使用camel case。使用秒而不是秒。 我希望这有帮助!

答案 2 :(得分:0)

您为什么使用getStringExtra()

您正在传递一个整数,因此接收一个整数

// retrieving the value
int seconds = getIntent().getIntExtra("key1");

如果你需要它作为字符串使用

String secondsAsString = String.valueOf(seconds);

答案 3 :(得分:0)

getStringExtra()替换为getIntExtra()

int default = -1;
String val = getIntent().getIntExtra("key1",default);
if(val == default){
  //Log, exception, no value etc.
}
String Seconds = String.valueOf(val);
byte[] secs = Seconds.getBytes();

Activity

中选择元素时,请致电开始ListView
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);
        }

答案 4 :(得分:0)

在OnItemSelectListner中使用Explicit Intent调用下一个Activity 试试这个

mSeconds.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

@覆盖

public void onItemSelected(AdapterView parentView,View selectedItemView,int

位置,长身份证){

               secondToUse = String.valueOf(position + 1);
               Intent intent = new Intent(CurrentClass.this,NewClass.Class);
               putInentExtra("Key1","secondTouse");
               startActivity(intent);

        }

在你的下一个活动中得到意图 -

意图i = getIntent();

String getPosition = i.getStringExtra(“Key1”);

享受:)