I have two activities i.e. MainActivity and NewActivity.On click of button in MainActivity,i am moving to NewActivity using Intent and passing some data using intent to NewActivity.
intent.putExtra("PUBLISHEDAT",newses.get(position).getTitle());
In NewActivity,i am checking whether the data received from Intent is null or not.If it is null i am setting the TextView visibility to GONE.But textview won't disappear and displays null even if data received from intent is null.
if(getIntent().getStringExtra("PUBLISHEDAT")==null || getIntent().getStringExtra("PUBLISHEDAT")==""){
publishedAtTextView.setVisibility(GONE);
}
Try this:
getIntent().getStringExtra("PUBLISHEDAT").trim().equals("")
or
getIntent().getStringExtra("PUBLISHEDAT").trim().isEmpty()
You should not use == to check for empty string. == compares the reference, not the value. Use this:
getIntent().getStringExtra("PUBLISHEDAT").isEmpty()
I think in putExtra part you put wrong value, maybe "null" as a string !
Check that with some hard coded values, like
"" "Null" and "Test" check your main activity, because in new activity you did it right
Related
I am on page 301 of this book and it is an example of an Activity getting "extras" from the intent that started it. I am fairly new to Java so maybe am missing something pretty obvious but...
I thought that when you declare a variable as "final" it meant that it doesn't change.
There is a line of code initialising a final variable:
public static final String EXTRA_MESSAGE="msg";
and then later in onCreate method:
tv.setText(getIntent().getStringExtra(EXTRA_MESSAGE));
The text displayed in the activity is not "msg" but is the string passed from the intent "I am the other activity". Why do you have to have the variable declaration above for the code to work? I don't understand what its doing.
Thanks
You are getting the extra received from another Activity indexed by the key 'msg'.
Like when you do this with the Intent used to start your Activity:
intent.putExtra("msg", "text going in the TextView");
The key is 'msg', but the value you get for the TextView is 'text going in the TextView'
Yes, final means EXTRA_MESSAGE value won't change, but you're not displaying EXTRA_MESSAGE value, but
getIntent().getStringExtra(EXTRA_MESSAGE)
which actually contains the value put in the previous activity. Regarding your question
Why do you have to have the variable declaration above for the code to work?
You don't actually need that variable for the code to work, but it's a good practice to use constant values instead of just hardcoding string values such in.-
getIntent().getStringExtra("msg")
The parameter you pass to getStringExtra is the key to which the String is mapped. All the extras you put in an Intent are mapped as key-value, so if you want to get a value you have to know the key, which must be the same key you used in the previous activity to save the value (with putStringExtra).
http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)
I get the list data from webservices (Json) in spinner. But before websevices data in spinner i want show null value.(Need first value is null)
please any one help me..
please give me a sample code.
Thank You.
You could add a dummy object representing your null value. But spinners are not designed for this - they should contain some data (or nothing if no data is available).
So in your case I would recommend using a ListView or something similar instead which could be opened in a new activity with startActivityForResult and return an Intent.
Are you doing whole process with single thread?
you can use your listener, first you inflate empty array/list and later you can set your data change..
A null value inside a spinner will give you a really annoying exception. It's better to add as first element of the array some object like "Please, select one" or "Nothing selected". When you create the array you give to the spinner, first add this element and then load the real data.
I am trying to open a list activity in two ways, (both from different activity A)
In one method I use putExtra(String name, String value) to populate the listActivity with sqlite.
This works perfectly awesome.
But now with another button, I am trying to open this list activity without putExtras (just viewing it) but i get a Force Close with Null Pointer Exception for this,
Can any one provide me some solutions for this, thanks in advance...
In your List activity, you're doing something like this yeah?
String passedValue = getIntent().getStringExtra("key");
In the case when you don't putExtras() , passedValue is null, so trying to use this string will give a NPE. So if you want to use passedValue, you might want to do this:
if(passedValue != null)
doSomethingWith(passedValue);
Im going a little crazy with this. In my app, i take a string which represents a bus stop, and then have an algorithm that matches it and displays its schedule. I needed to make that window an activity instead of a dialog and am using intents. Heres my code to send the intent:
Intent intent = new Intent(context, StopDialogActivity.class);
intent.putExtra("stop name", stopName);
context.startActivity(intent);
and heres my code to retrieve the string (in my onCreate):
Bundle extras = getIntent().getExtras();
departureStopName=extras.getString("stop name");
The string displays properly, but it isnt equal to a test string i have which is the same stop. The intent sends an integer over correct, what am i doing wrong with processing strings?
Make sure when comparing strings to use testName.equals(stopName) and not testName == stopName.
Using .equals() uses the equals method in the String class which compares the content. Using == compares the String Objects themselves, which need to be the same object in memory to evaluate to true.
Ah, my problem was while I use .equals() as a test to see if it was coming through okay, i was using == in the part of my code that broke.
hi all I have a variable in first activity i need to add values returned from second activity and store it in this variables. Switching between activities happens multiple times.... Any ideas..?
here is a piece of code which stores current value each time and not the Sum of it...
double quantity = Double.parseDouble(s1[1]);
double amount = Double.parseDouble(s1[2]);
if(s1[3].equals(""))
{
totalint = (quantity*(amount));
Log.d("hitherebbbbbbb",((Double)totalint).toString());
grandTotal =+totalint;
}
else
{
deduction = Double.parseDouble(s1[3]);
totalint = ((quantity*(amount-deduction*amount/100)));
Log.d("hitherebbbbbbb",((Double)totalint).toString());
grandTotal =+totalint;
}
If the amount of variables stays the same you could always use sharedpreferences. They are super simple to use and you can add a sharedpreference listener to update your activity when a value is changed. If the amount of variables changes (eg you have 3 integers at one time but maybe your user can change it so you need 5) then I would do something a little more complex that may not be the best option but I like it because I find it more simple than a database. Let me know which scenario best describes your situation and I'll get you more documentation.
You probably want to return the values from the second activity through its return intent and do a startActivityForResult() in the first activity.
you would do this by creating an intent in your second activity, setting any relevant return data, and calling
setResult(Activity.RESULT_OK, returnIntent);
finish();
You will be called back in the onActivityResult() method when the second activity is finished. You would then extract the values from the return intent and update your local storage.