Reputation: 14960
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, View v, int position,
long id) {
switch (position) {
case 1:
Intent newActivity1 = new Intent(this, BucketItemActivity.class);
newActivity1.putExtra("bucketno", position);
startActivity(newActivity1);
break;
case 2:
Intent newActivity2 = new Intent(this, BucketItemActivity.class);
newActivity2.putExtra("bucketno", position);
startActivity(newActivity2);
break;
}
}
The activity class that will receive it..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bucket_item);
String value = getIntent().getExtras().getString("bucketno");
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(value);
setContentView(textView);
}
But i always get a null in the String value...
Please help.
Upvotes: 15
Views: 59199
Reputation: 161
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");
Upvotes: 1
Reputation: 576
Bundle extras = getIntent().getExtras();
if (extras != null) {
String data = intent.getExtras().getString("key").toString();
}
Upvotes: 2
Reputation: 10395
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");
Upvotes: 15
Reputation: 6615
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");
Upvotes: 0
Reputation: 1
Use:
String value = getIntent().getExtras().get("key").toString();
getIntent().getExtras()
will give you Boundle. get()
method can be used to fetch the values.
Upvotes: 0
Reputation: 36289
String value = Integer.toString(getIntent().getExtras().getInt("bucketno"));
Upvotes: 4
Reputation: 54332
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.
Upvotes: 23