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

pass selected value to another class Android

我试图将从微调器中选择的值传递给另一个 class 但它传递了 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();

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

编辑: 我忘记在我的第一个回答中考虑到 getIntExtra() 需要 两个 个参数。它需要一个标识 "key" 和一个默认值。

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

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

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

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

原因: 因为你在 putExtra("key1", secondsToUse);

中放置了 int 而不是 String

此外,在命名变量时使用驼峰式大小写。使用秒而不是秒。 希望对您有所帮助!

为什么要使用 getStringExtra()

您传递的是一个整数,因此接收一个整数

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

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

String secondsAsString = String.valueOf(seconds);

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

当从 ListView

中选择元素时调用开始 Activity
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);
        }

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

mSeconds.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener() {

@Override

public void onItemSelected(AdapterView parentView, View selectedItemView, int

位置,长id){

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

        }

然后在您的下一个 Activity 中获取 Intent By-

意图 i= getIntent();

String getPosition= i.getStringExtra("Key1");

尽情享受吧:)