I'm scratching my head over this one. Basically what I am trying to do is I am trying to pass multiple values to the next activity, however when receiving the values in the next value, only the last one comes through.
In Activity A I have this
public void aberdeen1(View v) {
Intent i = new Intent(this, details.class);
Bundle extras = new Bundle();
extras.putString(storename, "storenamevalue");
extras.putString(address1, "streetaddress");
extras.putString(address2, "streetaddress2");
extras.putString(town, "City");
extras.putString(postcode, "AB12 3CD");
extras.putString(telnumber, "01234 567890");
extras.putString(faxnumber, "01234 567899");
i.putExtras(extras);
startActivity(i);
}
In Activity B I have this
Bundle extras = getIntent().getExtras();
String storename = extras.getString(StoreListA.storename);
storename1 = (TextView) findViewById(R.id.storename);
storename1.setText(storename);
String address1 = extras.getString(StoreListA.address1);
address01 = (TextView) findViewById(R.id.address1);
address01.setText(address1);
String address2 = extras.getString(StoreListA.address2);
address02 = (TextView) findViewById(R.id.address2);
String town = extras.getString(StoreListA.town);
town1 = (TextView) findViewById(R.id.town);
String postcode = extras.getString(StoreListA.postcode);
postcode1 = (TextView) findViewById(R.id.postcode);
String telnumber = extras.getString(StoreListA.telnumber);
telnumber1 = (TextView) findViewById(R.id.telnumber);
String faxnumber = extras.getString(StoreListA.faxnumber);
faxnumber1 = (TextView) findViewById(R.id.faxnumber);
}
all that shows for each textview is the fax number. need some help, I have tried other methods but this seemed to be the best way of doing it.
//Store Class
public static class Store implements Serializable{
String storeName;
String address1;
String address2;
String town;
String postCode;
String telNumber;
String faxNumber;
Store(String storeName,
String address1,
String address2,
String town,
String postCode, String telNumber, String faxNumber){
this.storeName = storeName;
this.address1 = address1;
this.address2 = address2;
this.town = town;
this.postCode = postCode;
this.telNumber = telNumber;
this.faxNumber = faxNumber;
}
}
public void aberdeen1(View v) {
Intent i = new Intent(this, details.class);
i.putExtra("store", new Store("storenamevalue",
"streetaddress",
"streetaddress2",
"City",
"AB12 3CD",
"01234 567890","01234 567899"));
startActivity(i);
}
//In Another Activity (Details).
//Whichever Activity you are coming from.
Activivty.Store store = (Activity.Store)getIntent()
.getExtras()
.getSerializable("store");
For more information on passing objects between Activities - Parcelable and Serializable
You should use your constants in both activities not only the one you are sending the data to. Also remember the key is first parameter in the putString call and the value is the second. It's hard to tell from your code which is the key and which is the value.
Ex:
extras.putString(StoreListA.storename, "This is the value for the name");
after double checking, the keys weren't unique, after a difficult day at work then coming home to that. It wasn't good, after having a cup of coffee this morning, I changed them and we are all good.
Related
i was developing an app and this question showed up:
EditText inputCorrect = (EditText) findViewById(R.id.inputCorrect);
EditText inputWrong = (EditText) findViewById(R.id.inputWrong);
EditText inputBlank = (EditText) findViewById(R.id.inputBlank);
EditText inputAll = (EditText)findViewById(R.id.inputAll);
String correctAmountText = inputCorrect.getText().toString();
String wrongAmountText = inputWrong.getText().toString();
String blankAmountText = inputBlank.getText().toString();
String allAmountText = inputAll.getText().toString();
myResultActivty.putExtra("c_a",correctAmountText);
myResultActivty.putExtra("w_a",wrongAmountText);
myResultActivty.putExtra("b_a",blankAmountText);
myResultActivty.putExtra("a_a",allAmountText);
startActivity(myResultActivty);
this is the code there are 4 edit texts with inputType of decimal number
i am getting string from them and send them to other activity.
in the second activity i take those string and turn them into ints with parseint method:
String correctNum = inputActivity.getString("c_a");
String wrongNum = inputActivity.getString("w_a");
String blankNum = inputActivity.getString("b_a");
String allNum = inputActivity.getString("a_a");
float res = 0;
int cN = Integer.parseInt(correctNum);
int wN = Integer.parseInt(wrongNum);
int bN = Integer.parseInt(blankNum);
int aN = Integer.parseInt(allNum);
but whenever i want to do mathematical operations it crashes or shows zero as result.
You could use intents to pass data between activities. In this case, you could create an intent like
Intent intent = Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key",10);
startActivity(intent);
In SecondActivity,
Bundle extras = getIntent().getExtras();
int yourNum = extras.getInt("key"); // you should get 10
I have tried work on this for whole day but could find a solution. I am working on a test google map app to learn java.
From this activity when button is clicked, it takes user to another activity and I need some values to be passed to next activity.
I am able to retrieve Start Address as well as destination address in next activity but not the distance.
implements GeoTask.Geo {
private TextView mFromAddress;
private TextView mToAddress;
String str_from,str_to;
int dist;
public void onClickBtn(View v){
str_from = mFromAddress.getText().toString();
str_to = mToAddress.getText().toString();
String url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=" + str_from + "&destinations=" + str_to + "&mode=driving&language=en-US&avoid=tolls&key=API-Key";
new GeoTask(MapsActivity.this).execute(url);
getResults();
}
// This is called from external class GeoTask.Java
public void setDouble(String result) {
String res[]=result.split(",");
Double min = Double.parseDouble(res[0])/60;
dist = Integer.parseInt(res[1])/1000;
}
public void getResults(){
Intent intent = new Intent(this, ResultsActivity.class);
String fromCity = str_from;
String toCity = str_to;
int kmDistance = dist;
intent.putExtra("from",fromCity);
intent.putExtra("to",toCity);
intent.putExtra("distance",kmDistance);
startActivity(intent);
}
}
I am retrieving these details by
Intent intent = getIntent();
String fromCity = intent.getStringExtra("from");
String toCity = intent.getStringExtra("to");
int kmDistance = intent.getIntExtra("distance", 0);
Reason why you have no distance.
Maybe the AsyncTask is not yet already done while calling the getResults();
Try to remove the getResults(); after calling the new GeoTask(MapsActivity.this).execute(url); and put the getResults(); in onPostExecute(args)
i am building an android app in this I want to get information how to read data from textView and to send an http request i was not able to figure out what is to be done to make it work.
Following is the code that i have used in intent section which will get the data from Edit text .
Intent summeryPage = new Intent(Form_section.this ,summry_page.class);
// ((routernBoltBeam)this.getApplication()).setFirstName(FirstName);
summeryPage.putExtra("fname",fName.getText().toString());
summeryPage.putExtra("lname",lName.getText().toString());
summeryPage.putExtra("date",date.getText().toString());
summeryPage.putExtra("email",emailAddress.getText().toString());
summeryPage.putExtra("mobuleNumber",mobileNumner.getText().toString());
summeryPage.putExtra("adults",adultsNumber.getText().toString());
summeryPage.putExtra("totalChildren",childensNumber.getText().toString());
summeryPage.putExtra("childAge",childrensage.getText().toString());
summeryPage.putExtra("hotelRooms",numberOfhotelRooms.getText().toString());
summeryPage.putExtra("departureCity",departureCity.getText().toString());
summeryPage.putExtra("destination",yourDesiredDestination.getText().toString());
summeryPage.putExtra("days",numberOfDays.getText().toString());
summeryPage.putExtra("Budget",yourBudget.getText().toString());
summeryPage.putExtra("selectHotel",preferHotel);
summeryPage.putExtra("airtickets",airticketPrefer);
summeryPage.putExtra("intercity",vehiclePreferance);
summeryPage.putExtra("travelType",travelPreferances);
summeryPage.putExtra("mealPlan",mealPreferances);
summeryPage.putExtra("addInfo",additionalInformation.getText().toString());
startActivity(summeryPage);
Following is the code which will set the data into the respective textView on other activity
public void showData(){
nameText =(TextView)findViewById(R.id.content);
viewDateText =(TextView)findViewById(R.id.showDate);
viewEmailText=(TextView)findViewById(R.id.showEmail);
viewMobileText=(TextView)findViewById(R.id.showMobileNumber);
viewTotalAdultText = (TextView)findViewById(R.id.showTotalAdults);
ViewTotalChildren =(TextView)findViewById(R.id.showTotalChildrens);
viewChildAge =(TextView)findViewById(R.id.showChildrensAge);
viewTotalRoomsText =(TextView)findViewById(R.id.showTotalRooms);
viewDepartureText =(TextView)findViewById(R.id.showDepartureCity);
viewDeatinationText =(TextView)findViewById(R.id.showDestination);
viewDaysText =(TextView)findViewById(R.id.showTotalDays);
viewBudgetText =(TextView)findViewById(R.id.showBudget);
viewPreferHotelText =(TextView)findViewById(R.id.showHotelPreferance);
viewAirticketText =(TextView)findViewById(R.id.showAirticketRequired);
viewIntercityText = (TextView)findViewById(R.id.showIntercityTravel);
viewTravelTypeText = (TextView)findViewById(R.id.showTraveType);
viewMealText =(TextView)findViewById(R.id.showMealPlan);
viewInfoText = (TextView)findViewById(R.id.showAdditionalInfo);
//String s =((routernBoltBeam)this.getApplication()).getFirstName();
Intent summery = getIntent();
String fName = summery.getStringExtra("fname");
String lName = summery.getStringExtra("lname");
String Date = summery.getStringExtra("date");
String Email = summery.getStringExtra("email");
String Mobile = summery.getStringExtra("mobuleNumber");
String totalAdults = summery.getStringExtra("adults");
String totalChildrens = summery.getStringExtra("totalChildren");
String ChildresnAge = summery.getStringExtra("childAge");
String TotalRooms = summery.getStringExtra("hotelRooms");
String Departure = summery.getStringExtra("departureCity");
String Destination = summery.getStringExtra("destination");
String TotalDays = summery.getStringExtra("days");
String TotalBudget = summery.getStringExtra("Budget");
String PreferHotel = summery.getStringExtra("selectHotel");
String SelectAirticket = summery.getStringExtra("airtickets");
String InterCityText = summery.getStringExtra("intercity");
String travelType= summery.getStringExtra("travelType");
String MealPlan = summery.getStringExtra("mealPlan");
String AddInfo = summery.getStringExtra("addInfo");
nameText.setText(fName +" "+lName);
viewDateText.setText(Date);
viewEmailText.setText(Email);
viewMobileText.setText(Mobile);
viewTotalAdultText.setText(totalAdults);
ViewTotalChildren.setText(totalChildrens);
viewChildAge.setText(ChildresnAge);
viewTotalRoomsText.setText(TotalRooms);
viewDepartureText.setText(Departure);
viewDeatinationText.setText(Destination);
viewDaysText.setText(TotalDays);
viewBudgetText.setText(TotalBudget);
viewPreferHotelText.setText(PreferHotel);
viewAirticketText.setText(SelectAirticket);
viewIntercityText.setText(InterCityText);
viewTravelTypeText.setText(travelType);
viewMealText.setText(MealPlan);
viewInfoText.setText(AddInfo);
}
so now what i want do is to read the data from these TEXTVIEWS and to send an HTTP REQUEST to an link and i am not able to understand how to figure it out
I intend to share 6 text information, however it always shows the last information only, i.e.
share.putExtra(Intent.EXTRA_TEXT, Contact);
I also tried to use a string array to store all 6 information, i.e:
share.putExtra(Intent.EXTRA_TEXT, stringArray);
However, it still doesn't work. Can anyone help ? Thank you.
My code:
public class SingleJobActivity extends Activity {
// JSON node keys
private static final String TAG_POSTNAME = "PostName";
private static final String TAG_LOCATION = "Location";
private static final String TAG_SALARY = "Salary";
private static final String TAG_RESPONSIBILITY = "Responsibility";
private static final String TAG_COMPANY = "Company";
private static final String TAG_CONTACT = "Contact";
String PostName;
String Location;
String Salary;
String Responsibility;
String Company;
String Contact;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_job_json_parsing);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
PostName = in.getStringExtra(TAG_POSTNAME);
Location = in.getStringExtra(TAG_LOCATION);
Salary = in.getStringExtra(TAG_SALARY);
Responsibility = in.getStringExtra(TAG_RESPONSIBILITY);
Company = in.getStringExtra(TAG_COMPANY);
Contact = in.getStringExtra(TAG_CONTACT);
// Displaying all values on the screen
TextView lblPostName = (TextView) findViewById(R.id.PostName_label);
TextView lblLocation = (TextView) findViewById(R.id.Location_label);
TextView lblSalary = (TextView) findViewById(R.id.Salary_label);
TextView lblResponsibility = (TextView) findViewById(R.id.Responsibility_label);
TextView lblCompany = (TextView) findViewById(R.id.Company_label);
TextView lblContact = (TextView) findViewById(R.id.Contact_label);
lblPostName.setText(PostName);
lblLocation.setText(Location);
lblSalary.setText(Salary);
lblResponsibility.setText(Responsibility);
lblCompany.setText(Company);
lblContact.setText(Contact);
// listeners of our button
View.OnClickListener handler = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.share:
shareTextUrl();
break;
}
}
};
// our button
findViewById(R.id.share).setOnClickListener(handler);
}
// Method to share either text or URL.
private void shareTextUrl() {
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
// Add data to the intent, the receiving app will decide
// what to do with it.
share.putExtra(Intent.EXTRA_SUBJECT, "Job Information:");
share.putExtra(Intent.EXTRA_TEXT, PostName);
share.putExtra(Intent.EXTRA_TEXT, Location);
share.putExtra(Intent.EXTRA_TEXT, Salary);
share.putExtra(Intent.EXTRA_TEXT, Responsibility);
share.putExtra(Intent.EXTRA_TEXT, Company);
share.putExtra(Intent.EXTRA_TEXT, Contact);
startActivity(Intent.createChooser(share, "Share via"));
}
}
Could anyone help ?
Concatenate the six strings into one larger string, and share that larger string.
You can Concatenate the individual strings to larger string, and can get it
Intent iin = getIntent();
Bundle b = iin.getExtras();
if (b != null) {
String j = (String) b.get("name");
JSONObject name = new JSONObject(j);
Textv.setText(name.getString("postName"));
TextvDesc.setText(name.getString("location"));
}
What's wrong with your actual code
Let's first understand why you only get the last piece of data.
Your problem is by convention in Java (and this also applies to the android.content.Intent.html#putExtra(String, String[]) method you are using) the methods "putXXX" replace the actual value (if it exists) with the new one you are passing.
This is similar to java.util.Map.html#put(K, V) method.
First possible solution
For your current code to work, you would have needed to use a different key for your extra data each time, that is, something like that:
share.putExtra(SingleJobActivity.EXTRA_TEXT_NAME, PostName);
share.putExtra(SingleJobActivity.EXTRA_TEXT_LOCATION, Location);
share.putExtra(SingleJobActivity.EXTRA_TEXT_SALARY, Salary);
share.putExtra(SingleJobActivity.EXTRA_TEXT_RESPONSIBILITY, Responsibility);
share.putExtra(SingleJobActivity.EXTRA_TEXT_COMPANY, Company);
share.putExtra(SingleJobActivity.EXTRA_TEXT_CONTACT, Contact);
This would work fine (assuming you declare as public static final the keys used, and you respect the Android contract for extra data keys, such as using the full package name for the key (e.g. public static final EXTRA_TEXT_NAME = "com.yourpackage.EXTRA_DATA_NAME";).
Second possible solution
Another way of doing it is to pass one extra with a String[] (see method documentation).
String[] extraParams = new String[6];
extraParams[0] = PostName;
extraParams[1] = Location;
extraParams[2] = Salary;
extraParams[3] = Responsibility;
extraParams[4] = Company;
extraParams[5] = Contact;
share.putExtra(SingleJobActivity.EXTRA_TEXT, extraParams);
Then in your new activity you retrieve this array using android.content.Intent.html#getStringArrayExtra(String) method.
Intent intent = getIntent();
String[] extraParams = intent.getStringArrayExtra(SingleJobActivity.EXTRA_TEXT);
I'm trying to send some data from one activity to another and it's sorta working but not like I want to work.
Problem 1-Things are getting mixed up. On the Next Activity part of the listitem is going to an incorrect textView and part to the correct textview.
Problem 2- I am only able to list 1 item on the new activity but I want to be able to send multiple listitems. I think the problem lies in combining different types of putExtra request to the same place like I do here.
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
Ant help would be appreciated.
Sending Data to next Activity
final TextView username =(TextView)findViewById(R.id.resultTextView);
String uname = username.getText().toString();
final TextView uplane =(TextView)findViewById(R.id.inputPrice);
String pick = uplane.getText().toString();
final TextView daplane =(TextView)findViewById(R.id.date);
String watch = daplane.getText().toString();
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)
.putExtra("Card Number",(CharSequence)uname)
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
);
finish();
This is the Next Activity
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getCharSequenceExtra("Card Number"));
}
Intent it = getIntent();
if (it.getCharSequenceExtra("date") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleTime);
setmsg.setText(it.getCharSequenceExtra("date"));
}
Intent id1 = getIntent();
if (id1.getCharSequenceExtra("inputPrice") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleName);
setmsg.setText(id1.getCharSequenceExtra("inputPrice"));
}
ArrayList<String> al= new ArrayList<String>();
al = getIntent().getExtras().getStringArrayList("list");
saleNotes= (TextView) findViewById(R.id.saleNotes);
saleNotes.setText(al.get(0));
Alright, a few things:
First of all you do not need to cast your strings as CharSequence.
Second thing,
Define intent, add your extras and only then call startActivity as below:
Intent intent = new Intent(MenuView1Activity.this,RecordCheckActivity.class);
intent.putExtra("date", watch);
startActivity(intent);
Third, when retrieving the intent create a bundle first as below:
Bundle extras = getIntent().getExtras();
String date = extras.getString("date");
EDIT:
Here is how you convert your entire array list to one single string and add it to your textview.
String listString = "";
for (String s : al)
{
listString += s + "\t"; // use " " for space, "\n" for new line instead of "\t"
}
System.out.println(listString);
saleNotes.setText(listString);
Hope this helps!
Try this, Don't use CharSequence just put string value
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",watch)
.putExtra("Card Number",uname)
.putExtra("inputPrice",pick)
.putStringArrayListExtra("list", listItems)
);
And get like this
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getStringExtra("Card Number"));
}