【问题标题】:pass selected value to another class Android将选定的值传递给另一个类 Android
【发布时间】:2015-08-22 11:43:41
【问题描述】:

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

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

【问题讨论】:

  • 将 secondsToUse 设为静态

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


【解决方案1】:

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

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

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

【讨论】:

  • @smithyy 如果您想超越职位,那么最好使用int 值。当然,您需要始终如一地使用它(所有变量都是int,您使用getIntExtra() 等)。让我更新一下我的答案。
  • @smithyy 看看我答案顶部的编辑。应该可以解决您的问题。
  • 它不允许我输入 putIntExtra() 它说无法解析方法
  • @smithyy 很抱歉,你是对的。请改用putExtra(),我已经调整了答案。
  • 它说当我输入int seconds = getIntent().getIntExtra("key1");时getIntExtra不能应用于字符串
【解决方案2】:

你为什么使用getStringExtra()

你传递一个整数,所以接收一个整数

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

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

String secondsAsString = String.valueOf(seconds);

【讨论】:

    【解决方案3】:

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

    mSeconds.setOnItemSelectedListener(new 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);
    
            }
    

    然后在你的下一个活动中获得 Intent By-

    意图 i= getIntent();

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

    享受:)

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 仍然作为 null 传递。我想要用户在微调器中选择的位置
      【解决方案5】:

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

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

      原因:因为您在putExtra("key1", secondsToUse); 中放入了int 而不是String

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

      【讨论】:

      • 当我得到错误 getIntExtra 并传入“key1”,因为它说它不兼容,值仍然作为 null 传递。我想要用户在微调器中选择的位置,然后传递它
      猜你喜欢
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 2014-02-26
      • 2018-02-05
      相关资源
      最近更新 更多