I need to get strings values from two different activities say activity1 and activity2, each activity should have maximum 4 edittext field..so totally eight fields should be displayed orderly in activity3. I have tried the code which is not displaying in the activity3.
Look at code,
Activity1
String namef = fname.getText().toString();
Intent first = new Intent(AssessmentActivity.this, Second.class);
first.putExtra("list1", namef);
startActivity(first);
String namel = lname.getText().toString();
Intent second = new Intent(AssessmentActivity.this, Second.class);
second.putExtra("list2", namel);
startActivity(second);
String phone = mob.getText().toString();
Intent third = new Intent(AssessmentActivity.this, Second.class);
third.putExtra("list3", phone);
startActivity(third);
String mailid = email.getText().toString();
Intent fourth = new Intent(AssessmentActivity.this, Second.class);
fourth.putExtra("list4", mailid);
startActivity(fourth);
Activity2
String cont = addr.getText().toString();
Intent fifth = new Intent(Second.this, Third.class);
fifth.putExtra("list5", cont);
startActivity(fifth);
String db = dob.getText().toString();
Intent sixth = new Intent(Second.this, Third.class);
sixth.putExtra("list6", db);
startActivity(sixth);
String nation = citizen.getText().toString();
Intent Seventh = new Intent(Second.this, Third.class);
Seventh.putExtra("list7", nation);
startActivity(Seventh);
String subject = course.getText().toString();
Intent Eight = new Intent(Second.this, Third.class);
Eight.putExtra("list8", subject);
startActivity(Eight);
*Activity3*
TextView first = (TextView)findViewById(R.id.textView2);
String fieldone = getIntent().getStringExtra("list1" );
first.setText(fieldone);
TextView second = (TextView)findViewById(R.id.textView3);
String fieldtwo = getIntent().getStringExtra("list2" );
second.setText(fieldtwo);
TextView third = (TextView)findViewById(R.id.textView4);
String fieldthree = getIntent().getStringExtra("list3" );
third.setText(fieldthree);
TextView fourth = (TextView)findViewById(R.id.textView5);
String fieldfour = getIntent().getStringExtra("list4" );
fourth.setText(fieldfour);
TextView fifth = (TextView)findViewById(R.id.textView6);
String fieldfive = getIntent().getStringExtra("list5" );
fifth.setText(fieldfive);
TextView sixth = (TextView)findViewById(R.id.textView7);
String fieldsix = getIntent().getStringExtra("list6" );
sixth.setText(fieldsix);
TextView seventh = (TextView)findViewById(R.id.textView8);
String fieldseven = getIntent().getStringExtra("list7" );
seventh.setText(fieldseven);
TextView eight = (TextView)findViewById(R.id.textView3);
String fieldeight = getIntent().getStringExtra("list8");
eight.setText(fieldeight);
I think you want to pass all of them into the new activity, not start a new activity with each.
Intent first = new Intent(AssessmentActivity.this, Second.class);
String namef = fname.getText().toString();
first.putExtra("list1", namef);
String namel = lname.getText().toString();
first.putExtra("list2", namel);
String phone = mob.getText().toString();
first.putExtra("list3", phone);
String mailid = email.getText().toString();
first.putExtra("list4", mailid);
startActivity(first);
Then when in your Second (the second activity)'s onCreate you can pull each of those and store them. Then when you're sending to the Third you can add each back in to the intent.
Intent fifth = new Intent(Second.this, Third.class);
fifth.putExtra("list1", namef_stored);
fifth.putExtra("list2", namel_stored);
fifth.putExtra("list3", phone_stored);
fifth.putExtra("list4", mailid_stored);
String cont = addr.getText().toString();
fifth.putExtra("list5", cont);
String db = dob.getText().toString();
fifth.putExtra("list6", db);
String nation = citizen.getText().toString();
fifth.putExtra("list7", nation);
String subject = course.getText().toString();
fifth.putExtra("list8", subject);
startActivity(fifth);
Related
I'm new on Android and I want to know how I can use Serializable in my code
First Activity:
#Override
public void onItemClick(Neighbour item) {
Intent i = new Intent(getActivity(), ProfilNeighbourActivity.class);
i.putExtra("avatar", item.getAvatarUrl());
i.putExtra("name", item.getName());
i.putExtra("city", item.getAddress());
i.putExtra("phone", item.getPhoneNumber());
i.putExtra("about", item.getAboutMe());
i.putExtra("fbUrl", item.getFbUrl());
startActivity(i);
}
Second Activity:
mAvatar = findViewById(R.id.profil_neighbour_image);
mName1 = findViewById(R.id.profil_neighbour_name1);
mName2 = findViewById(R.id.profil_neighbour_name2);
mCity = findViewById(R.id.profil_neighbour_city);
mPhone = findViewById(R.id.profil_neighbour_phone);
mAbout = findViewById(R.id.profil_neighbour_about);
mFbUrl = findViewById(R.id.profil_neighbour_fbUrl);
Intent i = getIntent();
String image = i.getStringExtra("avatar");
String name = i.getStringExtra("name");
String city = i.getStringExtra("city");
String phone = i.getStringExtra("phone");
String about = i.getStringExtra("about");
String fbUrl = i.getStringExtra("fbUrl");
Glide.with(this).asBitmap().load(image).fitCenter().into(mAvatar);
mName1.setText(name);
mName2.setText(name);
mCity.setText(city);
mPhone.setText(phone);
mFbUrl.setText(fbUrl + name);
mAbout.setText(about);
How can I shorten my code with Serializable please?
you can use passing objects around then Parcelable
more details : Parcelable
This is the first activity
Intent intent = new Intent(Songs.this,PlayingSong.class);
Bundle bundle=new Bundle();
bundle.putStringArray("data",songs);
intent.putExtras(bundle);
startActivity(intent);
This is the second activity
tv = (TextView)findViewById(R.id.playingSongName);
imge = (ImageView)findViewById(R.id.playingImage);
Bundle bundle = getIntent().getExtras();
String[] arrRecd = bundle.getStringArray("data");
tv.setText(arrRecd[]);
You can't set a String Array directly to the TextView. Instead create a String and set it to the TextView.
String text = "";
for(int i = 0; i < arrRecd.length; i++) {
text = text + arrRecd[i];
}
tv.setText(text);
In First Activity :
Intent intent=new Intent(this,SearchActivity.class);
intent.putStringArrayListExtra("data",songs);
startActivity(intent);
In second activity:
ArrayList<String> list=getIntent().getStringArrayListExtra("data");
I'm developing an app for android, and currently the only thing it does is calculate grades. There's two input boxes, one that takes the current grade, and another that takes a possible exam grade, and then it tells you the final grade. However, when there is no value in at least one of those boxes, it crashes. I tried to make an if statement to detect if the final value was null, but that didn't work. Here's my code:
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText grade = (EditText) findViewById(R.id.grade1);
EditText exam = (EditText) findViewById(R.id.grade2);
String stringGrade = (grade.getText().toString());
String stringExam = (exam.getText().toString());
double finalResult = (Double.parseDouble(stringGrade) * .8) + (Double.parseDouble(stringExam) * 0.2);
String finalResultString = String.valueOf(finalResult);
if (finalResultString == null){
finalResultString = "0";
} else {
intent.putExtra(EXTRA_MESSAGE, finalResultString);
startActivity(intent);
}}
and here's the code that renders it:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView1 = new TextView(this);
textView1.setTextSize(30);
textView1.setText("Your final would be " + message);
setContentView(textView1);
Just for reference, the .8 and .2 is because the current grade is weighted at 80%, and the exam is weighted at 20%. How can I make it so it won't crash when nothing is put into the boxes?
usernameEditText.getText().toString(); would not return null. It will return an empty string. What you can do before calculating finalResult is to check stringGrade and stingExam are non-empty and numerical values and if one is not then stop the operation. i.e.
String stringGrade = (grade.getText().toString());
String stringExam = (exam.getText().toString());
if (stringGrade.isEmpty() || stringExam.isEmpty()) {
Toast.makeText(this, "Invalid values", Toast.LENGTH_SHORT);
return;
}
try this:
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText grade = (EditText) findViewById(R.id.grade1);
EditText exam = (EditText) findViewById(R.id.grade2);
String stringGrade = (grade.getText().toString());
String stringExam = (exam.getText().toString());
if(stringGrade.equals("") ){
stringGrade = "0";
}
if(stringExam.equals("") ){
stringExam= "0";
}
double finalResult = (Double.parseDouble(stringGrade) * .8) + (Double.parseDouble(stringExam) * 0.2);
String finalResultString = String.valueOf(finalResult);
if (finalResultString == null){
finalResultString = "0";
} else {
intent.putExtra(EXTRA_MESSAGE, finalResultString);
startActivity(intent);
}}
I am trying to modify an Android app to suit my need.
The original app has page 1 to display list of notes, and page 2 displays the detailed note.
What I'm trying to achieve is instead of having only 1 textbox in the detailed note page, I want it to have several textboxes, and persist it as well.
Here is how I thought it would be (but failed miserably of course).
This the transmitter on the page 2 (detailed page) activity:
private void saveAndFinish()
{
EditText et = (EditText) findViewById(R.id.eventTitle);
String eventTitle = et.getText().toString();
EditText et2 = (EditText) findViewById(R.id.eventDate);
String eventDate = et2.getText().toString();
EditText et3 = (EditText) findViewById(R.id.eventVenue);
String eventVenue = et3.getText().toString();
EditText et4 = (EditText) findViewById(R.id.eventLocation);
String eventLocation = et4.getText().toString();
EditText et5 = (EditText) findViewById(R.id.eventNote);
String eventNote = et5.getText().toString();
EditText et6 = (EditText) findViewById(R.id.eventAttendees);
String eventAttendees = et6.getText().toString();
Intent intent = new Intent();
// pass these to the main activity will ya?
intent.putExtra("key", data.getKey());
intent.putExtra("title", eventTitle); // eventTitle is the edited text!
intent.putExtra("date", eventDate);
intent.putExtra("venue", eventVenue);
intent.putExtra("location", eventLocation);
intent.putExtra("note", eventNote);
intent.putExtra("attendees", eventAttendees);
setResult(RESULT_OK, intent);
// work done, go back to calling activity
finish();
}
This the receiver on the page 1 (main page) activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode==DETAIL_ACTIVITY_REQ && resultCode==RESULT_OK)
{
DataItem event = new DataItem();
event.setKey(data.getStringExtra("key"));
event.setTitle(data.getStringExtra("title"));
event.setDate(data.getStringExtra("date"));
event.setVenue(data.getStringExtra("venue"));
event.setLocation(data.getStringExtra("location"));
event.setNote(data.getStringExtra("note"));
event.setAttendees(data.getStringExtra("attendees"));
datasource.update(event);
refreshDisplay();
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
// which list view item was selected?
DataItem data = eventsList.get(position);
//now, to navigate. which class should i go to next?
Intent intent = new Intent(this, DetailedActivity.class);
// and also, pass data to the next activity will ya?
intent.putExtra("key", data.getKey());
intent.putExtra("title", data.getTitle());
intent.putExtra("date", data.getDate());
intent.putExtra("venue", data.getVenue());
intent.putExtra("location", data.getLocation());
intent.putExtra("note", data.getNote());
intent.putExtra("attendees", data.getAttendees());
// go go go!
startActivityForResult(intent, DETAIL_ACTIVITY_REQ);
}
Apparently only the "title" is saved, everything else is not. Help?
modify your code in saveAndFinish method
Intent intent = new Intent(thisActivityname.this,secondActivityname.class);
startActivity(intent);
Above code is put data and send to second activity.
now get above data in second activity
Intent intent = getIntent();
String title = intent.getStringExtra("title");
String date = intent.getStringExtra("date");
String venue = intent.getStringExtra("venue");
String location = intent.getStringExtra("location");
String note = intent.getStringExtra("note");
String attendees = intent.getStringExtra("attendees");
You must use getIntent() to get the data from Intent:
data = getIntent();
use on ActivityResult:
data = getIntent();
I have a way that I know is not the best way of sending them over rite now but I'm sending them as strings and converting them to an Int on the reciver side, the problem is when I do the conversion it crashes on my phone. This is what I have on my sender side:
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, PayTracker.class);
// hourly wage send
EditText editText = (EditText) findViewById(R.id.hourly_wage);
String message1 = editText.getText().toString();
intent.putExtra(MESSAGE_1, message1);
// ot wage send
EditText editText1 = (EditText) findViewById(R.id.ot_wage);
String message2 = editText1.getText().toString();
intent.putExtra(MESSAGE_2, message2);
// hours per day send
EditText editText2 = (EditText) findViewById(R.id.hours_day);
String message3 = editText2.getText().toString();
intent.putExtra(MESSAGE_3, message3);
// start new activity
startActivity(intent);
And this is what is on my reciving side:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_tracker);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Receive messages from options page
Intent intent = getIntent();
String message1 = intent.getStringExtra(Options.MESSAGE_1);
String message2 = intent.getStringExtra(Options.MESSAGE_2);
String message3 = intent.getStringExtra(Options.MESSAGE_3);
// convert string to integer
int HW = Integer.valueOf(message1);
int OTW = Integer.valueOf(message2);
int HPD = Integer.valueOf(message3);
Ive tested everything and its the conversion that is causing the app to crash, i was hoping somebody could help me make it not crash or give me a whole new way sending an int to my second activity insted of sending a string and converting it.
Thank you!
=======================================================================
Here is my new code with all your help!
Sending:
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, PayTracker.class);
// Gather text from text boxes
EditText editText = (EditText) findViewById(R.id.hourly_wage);
EditText editText1 = (EditText) findViewById(R.id.ot_wage);
EditText editText2 = (EditText) findViewById(R.id.hours_day);
//Create String from text
String message1 = editText.getText().toString();
String message2 = editText1.getText().toString();
String message3 = editText2.getText().toString();
//Convert String to Int
int HW = 0, OTW = 0, HPD = 0;
try{
HW = Integer.valueOf(message1);
OTW = Integer.valueOf(message2);
HPD = Integer.valueOf(message3);
}
catch(NumberFormatException nfe){
//do something else here
//for e.g. initializing default values to your int variables
}
// Send Integers to PayTracker.java
intent.putExtra(MESSAGE_HW, HW);
intent.putExtra(MESSAGE_OTW, OTW);
intent.putExtra(MESSAGE_HPD, HPD);
// start new activity
startActivity(intent);
}
Receiving side:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_tracker);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Receive messages from options page
Intent intent = getIntent();
int HW = intent.getIntExtra(Options.MESSAGE_HW, 0);
int OTW = intent.getIntExtra(Options.MESSAGE_OTW, 0);
int HPD = intent.getIntExtra(Options.MESSAGE_HPD, 0);
// set textview
TextView textView = (TextView) this.findViewById(R.id.yourpay);
textView.setText(String.valueOf(HW));
}
You don't need to pass Integers as Strings to be converted back in the receiving Activity. Intents can hold Integers as well as Strings.
Simply add your data like you normally would:
int foo = 5;
Intent intent = new Intent(this, bar.class);
intent.putExtra("foobar", foo);
And then retrieve your int from the intent in the receiving Activity as follows.
Intent intent = getIntent();
int foo = intent.getIntExtra("foobar", 0);
Intents can hold more data than just Strings. In fact, take a look at the documentation. You can see that Intents can hold Longs, Doubles, Floats, Parcelables, etc.
Pass data from one activity to another activity "String" value.useing intent
Activityone
Intent intent = new Intent(Activityone.this,Activitytwo.class)
intent.putExtra("TYPE", type);
startActivity(intent);
Activitytwo
Intent intent =getIntent();
Receivevalue =intent.getExtras().getString("TYPE");
(OR)
Receivevalue = getIntent().getExtras().getString("TYPE");
Pass data from one activity to another activity "Integer" value.useing intent
Activityone
Intent intent = new Intent(Activityone.this,Activitytwo.class)
intent.putExtra("TYPE", type);
startActivity(intent);
Activitytwo
Intent intent = getIntent();
value = intent.getIntExtra("TYPE", 0);
// Type = intent.getIntExtra(name, defaultValue);
Try something like this on the send side:
String message1 = editText.getText().toString();
intent.putExtra("MESSAGE_1", message1);
String message2 = editText1.getText().toString();
intent.putExtra("MESSAGE_2", message2);
String message3 = editText2.getText().toString();
intent.putExtra("MESSAGE_3", message3);
Receive side:
if (getIntent() != null) {
if (getIntent().getExtras() != null) {
String mess1 = getIntent().getExtras().getString("MESSAGE_1");
String mess2 = getIntent().getExtras().getString("MESSAGE_2");
String mess3 = getIntent().getExtras().getString("MESSAGE_3");
}
}
You can achieve your goal using following code
In Your Calling Activity
Intent value = new Intent(YourCallingActivity.this,DestinationActivity.class);
value.putExtra("integerone", integeronevalue);
value.putExtra("integertwo", integertwovalue);
value.putExtra("integerthree", integertwovalue);
startActivity(value);
In Destination Activity
int integerone = getIntent().getExtras().getInt("integerone");
int integertwo = getIntent().getExtras().getInt("integertwo");
int integerthree = getIntent().getExtras().getInt("integerthree");
Hope it helps
Encapsulate the conversion part within try/catch block, because your strings may not be convertible into integer values.
int HW, OTW, HPD;
try{
HW = Integer.valueOf(message1);
OTW = Integer.valueOf(message2);
HPD = Integer.valueOf(message3);
}
catch(NumberFormatException nfe){
//do something else here
//for e.g. initializing default values to your int variables
}
*Or more appropriately, pass the integer values in your sending part and receive them as it is.