I m using one EditText field and one spinner. I have to pass teh results of both to the next Activity. here, reqd_bloodgroup is the spinner item, i converted into String using:
reqd_bloodgrp = String.valueOf(spinner.getSelectedItem()); inside onItemSelected() of spinner.
intent.putExtra("city", citySelected.getText().toString());
intent.putExtra("bloodgroup", reqd_bloodgrp);
intent = new Intent(FindDonor.this,SpecificDonor.class);
startActivity(intent);
Here when i try to display these, there's no problem. They're correctly displayed. But when i try to fetch them in SpecificDonor activity, they show null values. The code used here is:
String text_city,text_bloodgroup;
text_city = getIntent().getStringExtra("city");
text_bloodgroup = getIntent().getStringExtra("bloodgroup");
Toast.makeText(getApplicationContext(), text_city + " " + "bloodgrp: " + text_bloodgroup, Toast.LENGTH_SHORT).show();
What could be the problem?
I think that you must do the:
intent = new Intent(FindDonor.this,SpecificDonor.class);
before adding extras. Try with:
intent = new Intent(FindDonor.this,SpecificDonor.class);
intent.putExtra("city", citySelected.getText().toString());
intent.putExtra("bloodgroup", reqd_bloodgrp);
startActivity(intent);
Related
I'm trying to use country code using CountryCodePicker library
https://github.com/hbb20/CountryCodePickerProject
And when i choose country from the list, i want that country code to display in EditText.
I dont know in which method to pass it.
Simple question, don't be angry :)
else {
String code = ccp.getSelectedCountryCodeWithPlus();
String number = edtPhone.getText().toString().trim();
String phoneNumber = "+"+ code + number;
Intent verify = new Intent (getActivity(), Verify.class);
verify.putExtra("phonenumber", phoneNumber);
verify.putExtra("phone", edtPhone.getText().toString());
verify.putExtra("name",edtName.getText().toString());
verify.putExtra("password",edtPassword.getText().toString());
verify.putExtra("edtSecureCode",edtSecureCode.getText().toString());
startActivity(verify);
}
It should pass EditText number to a second activity (full number included with country code, this is for firebase phone authentication, so it needs to know right number)
If I understand correctly, you want to set country code to your edittext; try to set yourEditText like in your code;
else {
String code = ccp.getSelectedCountryCodeWithPlus();
=> yourEditText.setText(""+code);
String number = edtPhone.getText().toString().trim();
String phoneNumber = "+"+ code + number;
Intent verify = new Intent (getActivity(), Verify.class);
verify.putExtra("phonenumber", phoneNumber);
verify.putExtra("phone", edtPhone.getText().toString());
verify.putExtra("name",edtName.getText().toString());
verify.putExtra("password",edtPassword.getText().toString());
verify.putExtra("edtSecureCode",edtSecureCode.getText().toString());
startActivity(verify);
}
String code = codepicker.getSelectedCountryCodeWithPlus();
// Although it is written with plus but it has only the country code except the plus.
// So, we need to add the "+" manually
editText.setText(""+code); // displays the code in the edit text
String number = phoneNo.getText().toString().trim(); //phoneNo that the user entered
String phoneNumber = "+" + code + number; // complete phone number
Intent verify = new Intent (getActivity(), Verify.class);
verify.putExtra("phonenumber", phoneNumber);
verify.putExtra("phone", edtPhone.getText().toString());
verify.putExtra("name",edtName.getText().toString());
verify.putExtra("password",edtPassword.getText().toString());
verify.putExtra("edtSecureCode",edtSecureCode.getText().toString());
startActivity(verify);
finish();
}
New to android, I'm currently trying to send a String value from one Activity to another. I have looked through several threads like How to use putExtra() and getExtra() for string data for an answer, but I cannot get it to work.
The string I want to send is:
public void golf(View view) {
Intent intent = new Intent(SearchSport.this, EventList.class);
intent.putExtra("type", "golf");
startActivity(intent);
and my receiver looks like
String type;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_list);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if ( extras != null)
type = extras.getString("type");
if (type == "golf"){
TextView eventName = (TextView) findViewById(R.id.EOName);
TextView eventTime = (TextView) findViewById(R.id.EOTime);
TextView eventLocation = (TextView) findViewById(R.id.EOLocation);
DatabaseOperations dop = new DatabaseOperations(ctx);
Cursor CR = dop.getInformation(1);
CR.moveToFirst();
eventName.setText(CR.getString(1));
eventTime.setText(CR.getString(7) + " " + CR.getString(8) + ". " + CR.getString(9) + " kl. " + CR.getString(10) + ":" + CR.getString(11));
eventLocation.setText(CR.getString(4));}
else {Toast toast = Toast.makeText(this, "Error in Type.", Toast.LENGTH_LONG);
toast.show();}
I have no Idea what's wrong here. The app displays the toast everytime I test it, instead of filling out the TextViews with the data from my database entry.
EDIT: Code has been changed based on answers, but the problem has not yet been solved. Writing if (type.equals("golf")){} instead of if (type == "golf"){} crashed the app.
EDIT 2: Problem solved! Fixed case sensitivity, used .equals instead of ==, and wrote the receiver as
if(getIntent().hasExtra("Type"))
type = getIntent().getStringExtra("Type");
In the end, it turns out is was the virtual device I used which crashed the app, for as of yet unknown reasons. When tested on an actual android phone, the app works as intented.
Thanks to everyone for their help!
try this
if(getIntent().hasExtra("Type"))
type = getIntent().getStringExtra("Type");
Change the key it is case sensitive :
type = extras.getString("Type");
Key is wrong when try to get String from Intent Extra :
type = extras.getString("type");
Replace with :
type = extras.getString("Type");
Note : Also use equals() instead == for comparing String
if (type.equals("golf")){
1) First keys are case sensitive
2) pass as
Bundle bundle = new Bundle()
bundle.putString("type","golf");
intent.putExtras(bundle);
after that get it using
Bundle received = i.getExtras();
received.getString("type");
As you are passing the value using Extra. You must have to catch that using getStringExtra(); and the Key must be case sensitive.
Intent i = getIntent();
type = i.getStringExtra("Type");
I have created two classes inside other class . inside these two classes i have used the Intent class.
Intent intent = new Intent(getApplicationContext(), DurationsActivity.class );
intent.putExtra("to",mydate);
in parent class i used this code to retrieve the intent value .
String to = getIntent().getExtras().getString("to");
String from = getIntent().getExtras().getString("from");
my logcat retriev this
java.lang.NullPointerException
mydate is like a key to identify the value in intent.
Declare mydate :-
String mydate,mydate1;
Intent intent = new Intent(ActivityName.this, DurationsActivity.class ); //Be Specific
intent.putExtra("to",mydate);
intent.putExtra("from",mydate1);
Than in DurationActivity.class
You can use your usual code in onCreate to get the contents from previous activity:-
String to = getIntent().getExtras().getString("to");
String from = getIntent().getExtras().getString("from");
Try, hope it will work
Intent intent = new Intent(YourCurrentActivity.this, DurationsActivity.class );
intent.putExtra("to",""+mydate);
String to = getIntent().getExtras().getString("to");
String from = getIntent().getExtras().getString("from");
Intent in = new Intent(MainActivity.this, Player.class);
String SongName = SpinSongSelector.getSelectedItem().toString ();
startActivity (in);
i wan to send SongName to next Activity
Try this in your first activity,
Intent intent = new Intent(MainActivity.this, Player.class);
String SongName = SpinSongSelector.getSelectedItem().toString ();
intent.putExtra("message", SongName);
startActivity(intent);
and then in second activity get your string like below
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
then set this string to text-view (This is not necessary but just for your reference, if you want to check whether you got correct string or not)
TextView txtView = (TextView) findViewById(R.id.your_resource_textview);
txtView.setText(message);
use this line before startActivty :
intent.putExtra("SongName",SongName);
then retrieve it from the target activty.
just use
first activity to send :
Intent in = new Intent(MainActivity.this, Player.class);
String SongName = SpinSongSelector.getSelectedItem().toString ();
in.putExtra("data",SongName);
startActivity (in);
to receive in another activity:
String Sname = getIntent().getExtras().getString("data");
to see the details of intents refer here : http://developer.android.com/guide/components/intents-filters.html
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"));
}