how i can pass element of edit text to another activity [duplicate] - android

This question already has answers here:
keeping a variable value across all android activities
(5 answers)
Closed 9 years ago.
im having a problem in passing information from one activity to another ... the idea is to choose from a vegetable market what do you need and choose the quantity this pic explain
Activity 1
i want to take all the information from the first activity so when i choose banana for example then put the quantity click on add to shopping cart the next activity would be like this
Activity 2
so how code i take the information from the first activity to the other ?!

When you start a new activity, you can send a bundle with it. Bundles can contain data like strings, booleans, ints, pretty much anything.
For example:
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("value_1", value1);
bundle.putInt("value_2", value2);
intent.putExtras(bundle);
this.startActivity(intent);
And in your second activities onCreate method, you can get the values from the bundle like:
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
Bundle bundle = getIntent().getExtras();
String value1 = bundle.getString("value_1");
String value2 = bundle.getString("value_2");

First associate EditText to a variable:
`EditText bananas = (EditText)findViewById(R.id.bananastext);`
Then get value of it:
`int value = Integer.parseInt(bananas.getText().toString());`
Then create an Intent and attach on it value:
`intent.put("bananas",value);`
Then from the other activity create a Bundle and get value:
Bundle extras = getIntent().getExtras;
Int value = extras.getInt("bananas");`

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()?

Android - Store data in Array of Objects and display later in ListView

I have two activities in my tab-bar application. One activity (activity1) is a normal activity and the other (activity2) is a ListActivity. In activity1, there is a button which stores every time I tap on the button data (date, value1,value2..) in an array of objects. I read that this is possible with Intents. But I dont want to start activity2 everytime I tap on the button.
How can I pass the array to activity2, so I can display them later after I tapped more than once on the button in my ListView?
EDIT :
Here is my code. But I get a NullPointerException, so it doesn't store correctly. My Object hat Serializable implemented.
Activity1:
Intent datenIntent = new Intent(this, LoggerActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("Daten",aDatensatz);
datenIntent.putExtras(bundle);
Activity2:
Bundle bundleData = this.getIntent().getExtras();
Datensatz[] aDatensatz = (Datensatz[]) bundleData.getSerializable("Daten");
You can pass an array in one shot from one activity to another in the following way :
Bundle myBundle = new Bundle();
b.putStringArray(key, array);
Intent i = new Intent(context, Class);
i.putExtras(myBundle);
And for receiveing
You can retrieve the value in another activity in following way:
Bundle b = this.getIntent().getExtras();
String[] array = b.getStringArray(key);
Hope this helps.

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 pass a random number from one class to another class?

I'm making Sudoku game. There are two classes: difficulty and Screen class. When the 'easy' button is pressed, I want to generate a random number from 1 to 9, and that numbers should go to the Screen class layout.
My 'easy' button is in choosedifficulty class layout.
You can pass information between activities using Intent extras. In your case, you could do (assuming I understood what you wanna do):
public void startSudoku(int chosenDifficulty) {
Intent i = new Intent(this, // Your current activity
SudokuActivity.class); // The activity showing the Sudoku
i.addExtra("com.example.sudoku.DifficultyLevel", chosenDifficulty);
startActivity(i);
}
Then, in the onCreate method of your Sudoku activity, you can get the difficulty level:
private int difficulty = 0;
protected void onCreate(Bundle savedInstanceState) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
difficulty = extras.getInt("com.example.sudoku.DifficultyLevel", 0);
}
}
Be carefull cause a sudoku is not only "random numbers from 1 to 9", so I recommend you to implement the algorithm properly. (Take a look at http://en.wikipedia.org/wiki/Sudoku_algorithms).
About passing data from one activity to another, you can use the intent. For example:
//In the place you launch your game
Intent myIntent = new Intent(myContext, Screen.class);
//sudoku is a String, for example, that contains the sudoku you want to pass
myIntent.putExtra("sudokukey", sudoku);
startActivity(myIntent);
And then, to retrieve the data:
Bundle bundle = getIntent().getExtras();
String mySudoku = bundle.getString("sudokukey");

Categories

Resources