I tried to send string from onclick recyclerview to the activity, all doing well except one of this.
GeneralItem generalItem = (GeneralItem) consolidatedList.get(position);
Intent intent = new Intent(getActivity(), DetailPengumuman.class);
intent.putExtra("getnama", generalItem.getDaftarPengumuman().getNama_p().toString());
Log.e("untaging","ada isinya : "+generalItem.getDaftarPengumuman().getNama_p().toString());
intent.putExtra("tanggalpengumuman", generalItem.getDaftarPengumuman().getTanggal_peng());
intent.putExtra("judulpengumuman", generalItem.getDaftarPengumuman().getJudul());
intent.putExtra("deskripsipengumuman", generalItem.getDaftarPengumuman().getDeskripsi());
startActivity(intent);
I also tried to log getnama in untaging tag its doing well and return me the data in log. But when I retrieve it in another activity, It always return null.
Intent intent = getIntent();
tanggalPengumumanGet = intent.getStringExtra("tanggalpengumuman");
judulPengumumanGet = intent.getStringExtra("judulpengumuman");
namaPengumumanGet = intent.getStringExtra("getnama");
deskripsiPengumumanGet = intent.getStringExtra("deskripsipengumuman");
Log.e("untaging","nama : " +namaMatkulGet);
You can first check for if intent contains data or not..
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if (bundle.containsKey("Key")) {
String value = bundle.getString("Key");
}
}
Trying Adding .toString() to call getStringExtra :
intent.putExtra("tanggalpengumuman", generalItem.getDaftarPengumuman().getTanggal_peng().toString());
intent.putExtra("judulpengumuman", generalItem.getDaftarPengumuman().getJudul().toString());
intent.putExtra("deskripsipengumuman", generalItem.getDaftarPengumuman().getDeskripsi().toString);
Solved thanks,
This because of I missing the String attribute
Intent intent = getIntent();
tanggalPengumumanGet = intent.getStringExtra("tanggalpengumuman");
judulPengumumanGet = intent.getStringExtra("judulpengumuman");
namaPengumumanGet = intent.getStringExtra("getnama");
deskripsiPengumumanGet = intent.getStringExtra("deskripsipengumuman");
Log.e("untaging","nama : " +namaMatkulGet);
I receiving extra in namaPengumumanGet and I log another String which is namaMatkkulGet
Related
I'm trying to pass strings from one activity to another.
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
String m = markerMap.get(marker.getId());
for(int i = 0; i < 8; i++) {
if(m.equals(name[i])) {
Intent intent = new Intent(MapsActivity.this, CustomInfoWindowAdapter.class);
intent.putExtra("FOOD_BANK",name[i]);
intent.putExtra("STREET_ADDRESS",address[i]);
intent.putExtra("WEBSITE",website[i]);
startActivity(intent);
}
}
}
});
That's the chunk of code that is supposed to send the strings to the other activity.
private void rendowWindowText(Marker marker, View view) {
Intent intent = getIntent();
String foodBank = getStringExtra("FOOD_BANK");
TextView tvTitle = (TextView) view.findViewById(R.id.title);
tvTitle.setText(foodBank);
String snippet = marker.getSnippet();
TextView tvSnippet = (TextView) view.findViewById(R.id.snippet);
String address = getStringExtra("STREET_ADDRESS");
tvSnippet.setText(address);
/*if(!title.equals("")) {
tvTitle.setText(title);
}*/
}
This bit is what should be receiving the data but getIntent() and getStringExtra() are deprecated. I've tried surpressing the deprecation with #SuppressWarnings("deprecation") before the method. I've tried restarting Android Studio and my computer with no avail. I've tried getActivity().getIntent(); and no luck either.
The message when I hover my cursor over the deprecated getIntent() is:
Cannot resolve method 'getIntent' in 'CustomInfoWindowAdapter'
Would really appreciate any ideas on how to fix this because I'm very new to Android Studio and Java in general.
To get the data which u sent, use the following code :
Bundle extras = getIntent().getExtras();
if (extras != null) {
String foodBank = extras.getString("FOOD_BANK");
String address = extras.getString("STREET_ADDRESS");
}
I tried to pass variable from one activity to another. In the main activity I managed to call back the values using Toast message.
MainActivity.java
search = (Button) findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View w) {
subreg = spinner_subregion.getSelectedItem().toString();
reg = spinner_region.getSelectedItem().toString();
pettype = spinner_pet.getSelectedItem().toString();
petnumber = spinner_num.getSelectedItem().toString();
Intent intent = new Intent(MainActivity, Result.class);
intent.putExtra("subregion", subreg);
intent.putExtra("region", reg);
intent.putExtra("petnum", petnumber);
intent.putExtra("pet", pettype);
startActivity(intent);
}
}
However, when I passed the value to Result.java, it only returns null. I tried searching for solutions and still does not work for me. Anyone knows how can I pass the data?
Result.java
Bundle extras = getIntent().getExtras();
if (extras!=null){
subreg = extras.getString("subreg");
reg = extras.getString("reg");
pettype = extras.getString("pettype");
petnumber = extras.getString("petnumber");
}
Try this
MainActivity.java
Intent myIntent = new Intent(this, NewActivity.class);
intent.putExtra("subregion", subreg);
intent.putExtra("region", reg);
intent.putExtra("petnum", petnumber);
intent.putExtra("pet", pettype);
startActivity(myIntent)
Result.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Intent intent = getIntent();
if(intent ==null){
........
.......
}else{
String subregion= intent.getStringExtra("subregion");
String region= intent.getStringExtra("region");
String petnum= intent.getStringExtra("petnum");
String pet= intent.getStringExtra("pet");
}
}
This is my code after changing it.
MainActivity.java
search = (Button) findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View w) {
subreg = spinner_subregion.getSelectedItem().toString();
reg = spinner_region.getSelectedItem().toString();
pettype = spinner_pet.getSelectedItem().toString();
petnumber = spinner_num.getSelectedItem().toString();
Intent intent = new Intent(Pet_sitting.this, Pet_sitting_result.class);
intent.putExtra("subreg", subreg);
intent.putExtra("reg", reg);
intent.putExtra("pettype", pettype);
intent.putExtra("petnumber", petnumber);
startActivity(intent);
}
});
Result.java
Intent intent = getIntent();
if (intent == null){
Toast.makeText(getApplicationContext(),"Null value passed",Toast.LENGTH_SHORT).show();
}
else{
subreg= intent.getStringExtra("subreg");
reg= intent.getStringExtra("reg");
pettype = intent.getStringExtra("pettype");
petnumber = intent.getStringExtra("petnumber");
Toast.makeText(getApplicationContext(),subreg + " " + reg + " " + pettype + " " + petnumber,Toast.LENGTH_SHORT).show();
}
As i can see you are using a Spinner in your code.
So it's Obvious a spinner can give you only one value at a time after Spinning the wheel...
you need to set the Code inside in null value Exception...
like...if it's null don't pass empty value inside intent
maybe it's worked...if it's then reply
MainActivity.java
if(subreg != null){
reg = spinner_region.getSelectedItem().toString();
intent.putExtra("reg", reg);
}
startActivity(intent);
Rsult.java
Intent intent = getIntent();
if (intent == null){
Toast.makeText(getApplicationContext(),"Null value passed",Toast.LENGTH_SHORT).show();
}else{
reg= intent.getStringExtra("reg");
Toast.makeText(getApplicationContext(),reg.toString,Toast.LENGTH_SHORT).show();
}
i have two EditText objects in my first activity. i want both their values when i go to the next activity.
Lets assume the EditText objects are inp1, inp2 and they can only accept numbers.
please mention how i can add their values to int Intent object and how i will extract their values in my next activity's .java file.
Here we go, your code will look like,
Sender Side:
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName1", intValue1);
myIntent.putExtra("intVariableName2", intValue2);
startActivity(myIntent);
Receiver Side:
Intent mIntent = getIntent();
int intValue1 = mIntent.getIntExtra("intVariableName1", 0);
int intValue2 = mIntent.getIntExtra("intVariableName2", 0);
Hope it helps.
Use this code
Intent intent = new Intent(first.this, Second.class);
Bundle extras = new Bundle();
extras.putString("value1",String.valueof(inp1.getText().toString()));
extras.putString("value2",String.valueof(inp2.getText().toString()));
intent.putExtras(extras);
startActivity(intent);
Then in your second Activity onCreate()
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String value1 = extras.getString("value1");
String value2 = extras.getString("value2");
To make thing easier and reusable you can make your own intent like this
public class MyIntent extent Intent{
private static final String FIRST_VALUE;
private static final String SECOND_VALUE;
public MyIntent(Context context, String firstValue, String secondValue){
super(context,MySecondActivity.class);
putExtra(FIRST_VALUE, firstValue);
putExtra(SECOND_VALUE, secondValue);
}
public String getFirstValue(){
getStringExtra(FIRST_VALUE);
}
public String getSecondValue(){
getStringExtra(SECOND_VALUE);
}
}
Sender:
startActivity(new MyIntent(this,"FirstString", "SecondString"));
Receiver Side:
MyIntent myIntent = (MyIntent)getIntent();
String firstValue = myIntent.getFirstValue();
String secondValue = myIntent.getSecondValue();
I've made an intent and i've put inside 2 extras
Intent intent = new Intent(MainActivity.this, Options.class);
TextView labelName = (TextView) findViewById(R.id.label1); // value = "Counter1"
TextView label2Name = (TextView) findViewById(R.id.label2); // value = "Counter 2"
String lblNameDefault = labelName.getText().toString();
String lbl2NameDefault = label2Name.getText().toString();
intent.putExtra(LABEL_NAME_DEFAULT, lblNameDefault);
intent.putExtra(LABEL_2_NAME_DEFAULT, lbl2NameDefault);
In my other activity i retrieve the info from them like this
//Get name from the label
Intent intent = getIntent();
String lblNameDefault = intent.getStringExtra(MainActivity.LABEL_NAME_DEFAULT);
String lbl2NameDefault = intent.getStringExtra(MainActivity.LABEL_2_NAME_DEFAULT);
//Set current name to editText
EditText labelNameDefault = (EditText)findViewById(R.id.set_name);
EditText label2NameDefault = (EditText)findViewById(R.id.set_name2);
labelNameDefault.setText(lblNameDefault, TextView.BufferType.EDITABLE);
label2NameDefault.setText(lbl2NameDefault, TextView.BufferType.EDITABLE);
The problem is that i receive to both labelNameDefault and label2NameDefault the result from LABEL_2_NAME_DEFAULT.
Can i only pass one extra?
How can i pass them both?
By default the value of labelName is "Counter 1" and the value of label2Name is "counter 2"
If i comment out intent.putExtra(LABEL_2_NAME_DEFAULT, lbl2NameDefault); the first label name is ok.
It looks like LABEL_2_NAME_DEFAULT is overwriting LABEL_NAME_DEFAULT
You could pass a 'bundle' of extras rather than individual extras if you like, for example:-
Intent intent = new Intent(this, MyActivity.class);
Bundle extras = new Bundle();
extras.putString("LABEL_NAME_DEFAULT",lblNameDefault);
extras.putString("LABEL_2_NAME_DEFAULT",lbl2NameDefault);
intent.putExtras(extras);
startActivity(intent);
Then in your Activity that your triggering, you can reference these like so:-
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String username_string = extras.getString("LABEL_NAME_DEFAULT");
String password_string = extras.getString("LABEL_2_NAME_DEFAULT");
Or (if you prefer):-
Bundle extras = getIntent().getExtras();
String username_string = extras.getString("LABEL_NAME_DEFAULT");
String password_string = extras.getString("LABEL_2_NAME_DEFAULT");
Hope this helps! :-)
You can pass two (or more) extras in an intent. You need to make sure that LABEL_2_NAME_DEFAULT and LABEL_NAME_DEFAULT does not have the same value, though.
activity one
Intent intent = new Intent(MainActivity.this, Options.class);
TextView labelName = (TextView) findViewById(R.id.label1); // value = "Counter1"
TextView label2Name = (TextView) findViewById(R.id.label2); // value = "Counter 2"
String lblNameDefault = labelName.getText().toString();
String lbl2NameDefault = label2Name.getText().toString();
intent.putExtra("LABEL_NAME_DEFAULT", lblNameDefault);
intent.putExtra("LABEL_2_NAME_DEFAULT", lbl2NameDefault);
activity two
username_string = getIntent().getExtras().getString("LABEL_NAME_DEFAULT");
password_string = getIntent().getExtras().getString("LABEL_2_NAME_DEFAULT");
My bundle is returning null strings for the contained extras. Not NPEs, actual "null" values. Any ideas on why this would be happening?
new bundle
String u = null;
Bundle b = new Bundle();
Intent i = new Intent(view.getContext(), ******.class);
u = api.companyData.link.get(position);
Log.d("URL++++++++++++++++++++", u);
b.putString("graphic", api.companyData.graphic);
b.putString("name", api.companyData.name);
b.putString("url", u);
i.putExtras(b);
startActivity(i);
The log statement is returning the url fine.
Receiver of bundle
Bundle extras = getIntent().getExtras();
if(extras !=null) {
Log.d("EXTRAS", extras.getString("name")+extras.getString("graphic")+extras.getString("link"));
D/EXTRAS ( 4698): nullnullnull
I always do it this way:
Intent i = new Intent(view.getContext(), ******.class);
i.putExtra("url", u);
and then
String url = getIntent().getStringExtra("url");
I've never tried it your way, but if you look at the Android docs for putExtras(Bundle), it says:
Add a set of extended data to the intent. The keys must include a
package prefix, for example the app com.android.contacts would use
names like "com.android.contacts.ShowAll".
That you're not doing that may be the reason for the failure.
put the objects in the intent directly, e.g.
Intent i = new Intent(view.getContext(), ******.class);
i.putExtra("graphic", ...);
i.putExtra("name", ...);
i.putExtra("url", ....);
Then in the receiver activity:
getIntent().getStringExtra("graphic");
getIntent().getStringExtra("name");
getIntent().getStringExtra("url");