How to pass value using Intent between Activity in Android?

前端 未结 7 1474
面向向阳花
面向向阳花 2020-12-01 16:48

I want to pass the value of the position in one activity class to another...

My code is as follows:

protected void onListItemClick(ListView listView,         


        
相关标签:
7条回答
  • 2020-12-01 17:20

    You can use :

    In first activity ( MainActivity page )

    Intent i = new Intent(MainActivity.this,SecondActivity.class); 
    i.putExtra("YourValueKey", yourData.getText().toString());
    

    then you can get it from your second activity by :
    In second activity ( SecondActivity page )

    Intent intent = getIntent();
    String YourtransferredData = intent.getExtras().getString("YourValueKey");
    
    0 讨论(0)
  • 2020-12-01 17:28

    From the First Activity

    Intent i=new Intent(getApplicationContext(),BookPractionerAppoinment.class);
    i.putExtra("prac","test");
    startActivity(i);
    

    Getting values in Second ACT Activity

    String prac=getIntent().getStringExtra("prac);
    

    For Serialized objects:

    Passing

    Intent i=new Intent(getApplicationContext(),BookPractionerAppoinment.class);
     i.putExtra("prac",pract);
     startActivity(i);
    

    Getting

    pract= (Payload) getIntent().getSerializableExtra("prac");
    
    0 讨论(0)
  • 2020-12-01 17:37
    String value = Integer.toString(getIntent().getExtras().getInt("bucketno"));
    
    0 讨论(0)
  • 2020-12-01 17:43

    Use:

    String value = getIntent().getExtras().get("key").toString();
    

    getIntent().getExtras() will give you Boundle. get() method can be used to fetch the values.

    0 讨论(0)
  • 2020-12-01 17:46

    In addition who has different sittuation

      double userDMH = Util.getInstance(TakeInfoOne.this).calculateBMH(userGender, kg, talll, currentAge, activityy);
                            Intent intentTogeneral = new Intent(TakeInfoOne.this, TakeInfoTwo.class);
                            intentTogeneral.putExtra("user_current_age",userDMH );
                            startActivity(intentTogeneral);
    

    If you put other primitives like double , boolean , int just dont forget to type correct format while geting the value in secont Activity

    Bundle extras = getIntent().getExtras();
        if (extras != null) {
            double value =  extras.getDouble("user_current_age");
            txt_metabolism.setText(""+value);
        }
    

    Here with extras.getDouble() type your concerned value type like

    extras.getInt("user_current_age");
    extras.getBoolean("user_current_age");
     extras.getString("user_current_age");
    
    0 讨论(0)
  • 2020-12-01 17:47

    Replace this,

    String value = getIntent().getExtras().getString("bucketno");
    

    with

    int value = getIntent().getExtras().getInt("bucketno");
    

    You are trying to pass int value but retrieving String Data. That's why you are getting the nullpointerException.

    0 讨论(0)
提交回复
热议问题