Android pass value Activity - android

I want to pass a value from the ListView Activity 1 Activity 2 for editing.
I have this code but the value is not passed in the second Activity.
ACTIVITY A
Intent i = new Intent(this, Modifica_entrate.class);
Bundle extras = new Bundle();
extras.putString (tv1.getText().toString(), data);
i.putExtras(extras);
ACTIVITY B
Bundle extras = getIntent().getExtras();
String valuePass = extras.getString("data");
mDataScelta.setText(i.getExtras().getString(valuePass));

You are mixing up keys and values a bit too much.
The first parameter here:
extras.putString (tv1.getText().toString(), data);
Must match the parameter here:
String valuePass = extras.getString("data");
So the code you have there puts a String with the key tv1.getText().toString() that is, it takes the text you entered in the textbox and uses it as a key (which is probably not what you intended to do). For this key, you are putting the value of the variable data. Then you try to retrieve the key "data" (note also that data and "data" are not the same thing).
So what you want is probably:
extras.putString("data", tv1.getText().toString());
And then you can retrieve it like this:
mDataScelta.setText(i.getStringExtra("data"));

Related

Collecting data in multiple activities and to display those values in final activity for preview in android

Can any one please help me ..!! I want to collect some details in 3 forms i.e,(activity) and at final i need to display the data collected, for preview. Eg: In first activity I am entering the Personal details, then in the second activity entering the Address Details and in third activity entering the qualification details. After the third activity I want to display the data entered in all the three activity in a fourth activity for the preview purpose .. I have searched for the solution only for passing data between only 2 activities .. i need that for multiple activities.
Thank You
// on the first activity when you are fill all the data then
//And just about to go on second activity through intent then pass all the values like this and get it on second and so on...
Intent intent = new Intent(getApplicationContext(), DisplayActivity.class);
//Create a bundle object
Bundle b = new Bundle();
//Inserts a String value into the mapping of this Bundle
b.putString("name", name.getText().toString());
//Add the bundle to the intent.
intent.putExtras(b);
//start the DisplayActivity
startActivity(intent);
//On the second activity get the first
Value of form data
Bundle b = getIntent().getExtras();
b.getCharSequence("name");
Let's say you have some POJO called Preview and you have three other Java Class to store values from each Activity;
public class Preview implements Serializable{
private Activity1Data data1;
private Activity2Data data2;
private Activity3Data data3;
//create getter and setter for all
//.........
}
Then on each Activity, you will set their value to corresponding field on instance of Preview i.e. set data1 value collected from Activity1.
While calling other Activity you'll pass this instance of preview as
nextIntent.putExtra("PreviewObject", yourPreviewInstance);
And in intent you'll receive using:
yourPreview = (Preview)getIntent().getSerializableExtra("PreviewObject");
Once you're done collecting data in this Activity, set the value collected to corresponding attribute.May be in this case it will be data2, since we updated data1 earlier. At this point, you have data1 and data2 from
two Activity.
Now pass this in similar way and you can use it when it reaches the final intent. At final intent, you have all data you need for the Preview filled.

How to pass Multiple information from one screen to another screen in android

I have a sign-up form, and I have to pass all info filled in that form to another screen. I know how to display if there is one field, but in sign-up form there are multiple fields. so I want to know how to display all the info.
If you are launching a new activity, just create a Bundle, add your values, and pass it into the new activity by attaching it to the Intent you are using:
/*
* In your first Activity:
*/
String value = "something you want to pass along";
String anotherValue = "another something you would like to pass along";
Bundle bundle = new Bundle();
bundle.putString("value", value);
bundle.putString("another value", anotherValue);
// create your intent
intent.putExtra(bundle);
startActivity(intent);
/*
* Then in your second activity:
*/
Bundle bundle = this.getIntent().getExtras();
String value = bundle.getString("value");
String anotherValue = bundle.getString("another value");
To pass User data(multiple info) from one screen to another screen :
Create a model for user with setter and getter method.
make this class Serializable or Parcelable (Prefer) .
Create object of user class and set all data using setter method.
Pass this object from one activity to another by using putSerializable.
Person mPerson = new Person();
mPerson.setAge(25);
Intent mIntent = new Intent(Activity1.this, Activity2.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable(SER_KEY,mPerson);
mIntent.putExtras(mBundle);
startActivity(mIntent);
And get this object from activity 2 in on create methode.
Person mPerson = (Person)getIntent().getSerializableExtra(SER_KEY);
and SER_KEY will be same.
for more detail please go to this link:
http://www.easyinfogeek.com/2014/01/android-tutorial-two-methods-of-passing.html
I hope it will work for you.
You can make use of bundle for passing values from one screen to other
Passing a Bundle on startActivity()?

Passing a String value across Activities?

I am currently attempting to pass a string across activities. The string is used to set an image in the second activity. So the user is clicking a button in the first activity and then the appropriate image is loaded in the second activity based upon what was selected. There are three buttons in the first activity all which should send a String value to the next activity.
This is what I have in my first activity:
Button archerButton = (Button) findViewById(R.id.Button_Archer);
archerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String selection = "archer";
Bundle basket = new Bundle();
basket.putString("key", selection);
Intent i = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
i.putExtras(basket);
startActivity(i);
finish();
}
});
}
The receiving activity has the following code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.level_one);
ImageView img = (ImageView) findViewById(R.id.Character_Chosen);
Bundle extras = getIntent().getExtras();
String Toon = extras.getString("key");
if(Toon=="archer"){
img.setImageResource(R.drawable.archer);
}
if(Toon=="mage"){
img.setImageResource(R.drawable.mage);
}
if(Toon=="warrior"){
img.setImageResource(R.drawable.warrior);
}
}
I know that the receive if statements section works because if I manually set the value Toon the image will load. So somewhere the information is not being sent or read properly. I followed a tutorial online and but am stumped on this.
issue is Java 101. string equality test is done using the equals method.
Toon.equals("archer");
Extra tip : inverse your test to avoid null pointer exception :
"archer".equals(Toon);
Extra extra tip : considering your cases are mutually exclusive, use if else if to avoid testing all cases.
You are comparing your strings wrong,
Toon=="Anything"
will never be true, you need to use
Toon.equals("Anything")
for it to work.
EDIT:
This is true because "==" will just compare the memory addresses of the Strings, and "Anything" will just have been allocated and definitely won't match Toon. .equals however actually compares the chars in the string.
Also to test things like this try putting in a
Log.i("YourAppName", "Toon value is: " + Toon);
after
String Toon = extras.getString("key");
try to use SharedPreferences, Set it after/with the button-click and get it on 2. Activity OnCreate
If you just want to pass a simple String, you can use the following:
On sending:
Intent i = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
i.putExtra("key", selection);
startActivity(i);
On receiving:
String selection = getIntent().getStringExtra("key");
If there is no parameter with this key, selectionwill be null.
Try this
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("email", "lol#hotmail.com");
startActivity(i);
and
Intent i = getIntent();
String myemail = i.getStringExtra("email");
Furthermore, to compare String, use .equals() not ==

How to get results in new activity from the previous activity results in android?

I have two activities say X and y. In x there is edittext n 6 radiobutton if user clicks button the value gets retrieved from database based on input from edittext n radiobutton. the values should be displayed in next activity y. can yu pls help in giving snippet..thanks in advance
You can easily pass data from one activity to another using Bundle or Intent.
Lets look at the following example using Bundle:
//creating an intent to call the next activity
Intent i = new Intent("com.example.NextActivity");
Bundle b = new Bundle();
//This is where we put the data, you can basically pass any
//type of data, whether its string, int, array, object
//In this example we put a string
//The param would be a Key and Value, Key would be "Name"
//value would be "John"
b.putString("Name", "John");
//we put the bundle to the Intent
i.putExtra(b);
startActivity(i, 0);
In "NextActivity" you can retrieve the data using the following code:
Bundle b = getIntent().getExtra();
//you retrieve the data using the Key, which is "Name" in our case
String data = b.getString("Name");
How about using only Intent to transfer data. Lets look at the example
Intent i = new Intent("com.example.NextActivity");
int highestScore = 405;
i.putExtra("score", highestScore);
In "NextActivity" you can retrieve the data:
int highestScore = getIntent().getIntExtra("score");
Now you would ask me, whats the difference between Intent and Bundle, they seems like
they do exactly the same thing.
The answer is yes, they both do exactly the same thing. But if you want to transfer alot of data, variables, large array you would need to use Bundle, as they have more methods for transferring large amount of data.(i.e. If your only passing one or two variable then just go with Intent.
You should put the values into a bundle and pass that bundle to the intent that's starting the next activity. Sample code is in the answer to this question: Passing a Bundle on startActivity()?
Bind the data that you would like to send with the Intent you are calling to go to the next activity.
Intent i = new Intent(this, YourNextClass.class);
i.putExtra("yourKey", "yourKeyValue");
startActivity(i);
In YourNextClass activity, you can get the passed data by using
Bundle extras = getIntent().getExtras();
if (extras != null) {
String data = extras.getString("yourKey");
}

How to send data from main activity to other activity?

How to send more than 1 data with bundle ?
If only one :
String status = txtStatus.getText().toString();
String txtstatus = String.valueOf(status);
Bundle bundle = new Bundle();
bundle.putString("status", txtstatus);
a.putExtras(bundle);
startActivityForResult(a, 0);
if more than 1 data ??
String status = txtStatus.getText().toString();
String txtstatus = String.valueOf(status);
String confirm = txtConfirm.getText().toString();
String txtconfirm = String.valueOf(confirm);
what's next ??
just keep adding in bundle as you are adding bundle.putString("status", txtconfirm );
and when you are done set this bundle to intent:a.putExtras(bundle);
If I understood the question, this should be fine:
Bundle bundle = new Bundle();
bundle.putString("status", txtstatus);
bundle.putString("confirm", txtconfirm);
for more than one data
String status = txtStatus.getText().toString();
String txtstatus = String.valueOf(status);
String confirm = txtConfirm.getText().toString();
String txtconfirm = String.valueOf(confirm);
Bundle bundle = new Bundle();
bundle.putString("status", txtstatus);
bundle.putString("confirm",txtconfirm);
a.putExtras(bundle);
startActivityForResult(a, 0);
Just put your 2nd string in with bundle.putString() making sure you use a unique key name for it.
The process of serializing/parceling custom objects, attaching to the bundle with keys and undoing all this at the other end gets tedious when you have a lot of data or/and when the data needs to serve different purposes/functions in the launched Activity etc.
You can check out this library(https://github.com/noxiouswinter/gnlib_android/wiki#gnlauncher) I wrote to try and solve this problem.
GNLauncher makes sending objects/data to an Activity from another Activity etc as easy as calling a function in the Activity with the required data as parameters. It introduces type safety and removes all the hassles of having to serialize, attaching to the intent using string keys and undoing the same at the other end.
You can also trigger different functionalities in the Activity directly by choosing which method to launch into with the data.

Categories

Resources