Reputation: 37
I am new with Android Studio. I am working on following code but it is not working, kindly help me out.
I am trying to send an int value but emulator stopped working every time.
From class page1 to page2. On page1:
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(page1.this, page2.class);
Bundle b = new Bundle();
b.putInt("key", 1); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
}
});
On page2: I am writing this code in onCreate function.
Bundle coming = getIntent().getExtras();
int value = coming.getInt("key");
paper.setText(value);
Upvotes: 0
Views: 1330
Reputation: 1329
There are few ways to do it, It is depend on your requirement. I'll show how it can be done,
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), page2.class);
intent.putExtra("key", 1); //Your id
startActivity(intent);
}
});
and you can receive data back in page2 Activity as
int id = getIntent().getIntExtra("key",1);
Can use bundle for pass bundle of data
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), page2.class);
//Create the bundle
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("key", 1);
//Add the bundle to the intent
intent.putExtras(bundle);
startActivity(intent);
}
});
receive data from page2 Activity
Bundle bundle = null;
bundle = this.getIntent().getExtras();
String keyString = bundle.getString("Name");//this is for String
SharedPreferences sp =getSharedPreferences(logFile, 0);
SharedPreferences.Editor editor = sp.edit();
editor.putString("id", 1);
editor.commit();
And can retrive it from next page
Upvotes: -1
Reputation: 13368
Before setText convert value int to String
paper.setText(String.valueOf(value));
Upvotes: 0
Reputation: 10697
You need to set a text value to your TextView
Bundle coming = getIntent().getExtras();
int value = coming.getInt("key");
// Add an empty string to value
paper.setText(value + "");
Upvotes: 0
Reputation: 4490
You have to cast int
to String
as you are trying to set it to TextView
So change:
paper.setText(value);
to:
paper.setText(String.valueOf(value));
Upvotes: 2