The value in string.xml file...
<string name="bangla_history_2ndpoint">SOME VALUE </string>
From this activity i am trying to pass value to another activity ... by using putextra
Intent ptwo=new Intent("com.powergroupbd.victoryday.of.bangladesh.HISTORYDESCRIPTION");
ptwo.putExtra("header", R.string.bangla_history_2ndpoint);
startActivity(ptwo);
Then get the value in this activity ...
But it is not getting the value from the string.xml file...
text_point = getIntent().getStringExtra("header");
Toast.makeText(getApplicationContext(), text_point, Toast.LENGTH_LONG).show();
But it is toasting blank ....
Please give a solution...
That's because you're trying to retrieve a String, but what you're passing in as extra is actually the resource identifier of it, an int. Either put an actual string as extra, or retrieve an int on the receiving end to fix this.
// put:
ptwo.putExtra("header", R.string.bangla_history_2ndpoint);
// get:
int extraResourceId = getIntent().getIntExtra("header");
text_point = getString(extraResourceId);
Or:
// put:
ptwo.putExtra("header", getString(R.string.bangla_history_2ndpoint));
// get:
text_point = getIntent().getStringExtra("header");
Related
I want to use an intent string extra to dynamically determine what String array in an XML file to use.
Java code:
Intent myIntent = getIntent();
String stringID = myIntent.getStringExtra("stringID");//pull string id
String[] allStrings = getResources().getStringArray(stringID);
The XML:
<string-array name="set1">
<item>item1</item>
<item>item2</item>
<item>item3</item>
</string-array>
The last line in the java code doesn't work because it wants something like r.array.set1, but I want to choose this dynamically instead. How can I accomplish this? Would it be easier to use the ID of the string array somehow?
Actually r.array.set1 is a reference to an Integer. So you should pass it as an Integer, not a String. So:
Intent myIntent = getIntent();
int intID = myIntent.getIntExtra("stringID", r.array.set1);//The second parameter is the default value if nothing is specified
String[] allStrings = getResources().getStringArray(intID);
And in the other activity pass it as an Integer:
intent.putExtra("stringID", r.array.set1);
I´ve got the problem, that Android Studio reminds me to use the string.xml for set the text. The following gives me the hint: Do not concatenate text displayed with setText. Use resource string with placeholders.
public int points = 0;
public TextView tv_points;
tv_points.setText("Points: " + points);
Okay if i use the string xml with this code it does not what i want:
<string name="points_string">Points: </string>
public int points = 0;
public TextView tv_points;
tv_points.setText((R.string.points_string) + points);
It brings no error and no hint, but is wrong. I do not get the wanted effect to set the points.
You need to format the string using strings.xml like this:
<string name="points_string">Points: %1$d</string>
Then you can use it like this:
tv_points.setText(getResources().getString(R.string.points_string, points));
I'm writing an app that takes in an input from the AddBook class, allows it to be displayed in a List, and then allows the user to Search for their book. To this end, I'm creating a temporary EditText, binding it to the box where the user actually enters their search value, then (after ensuring that it is not empty) I compare what they've entered for the ISBN number with the ISBN numbers of each entry in the arrayList of <Book> custom objects, the list being named books.
Problem is, when I try to parse the EditText into an Int, it doesn't seem to work. I first tried using toString() on the EditText, then using Integer.parseInt() on the result of that method, but it hasn't worked out, as the conversion is seemingly unsuccessful;
All of the resources are in place and the code compiles properly, so those aren't the problems here.
EditText myEdTx = (EditText) findViewById(R.id.bookName);
if(myEdTx.getText().equals("")){Toast toast = Toast.makeText(this, "Please enter something for us to work with!", Toast.LENGTH_SHORT);
toast.show();}
else{
//resume here
for(int i=0; i<books.size(); i++)
{
Book tBook = new Book(i, null, null); tBook = books.get(i); String s=myEdTx.toString();
int tInt = Integer.parseInt(s);`
To get the string representation of an EditText's content, use:
String s = myEdTx.getText().toString();
Using toString() directly on the EditText gives you something like:
android.widget.EditText{40d31450 VFED..CL ......I. 0,0-0,0}
...which is clearly not what you want here.
You assume the user inputs a number into the text field, but that is unsafe, as you only get a string text (which theoretically can contain non-numbers as well). When I remember correctly, you can adjust a text field in android where a user only can input numbers, which should suit you more.
NumberFormatException occurs when Integer.parse() is unable to parse a String as integer, so, its better to Handle this exception.
String s = myEdTx.getText().toString();
try {
int tInt = Integer.parseInt(s);
} catch( NumberFormatException ex ) {
//do something if s is not a number, maybe defining a default value.
int tInt = 0;
}
So the current String here you are trying to parse is with white space in the line
and integer class unable to parse that white space. So use following code.
String s=myEdTx.getText().toString();
int tInt = Integer.parseInt(s.trim());
String s = myEdtx.getText().toString().trim();
int iInt = Integer.parseInt(s);
I have an Android App calling a PHP script, which returns 0 (FALSE) or 1 (TRUE) - unfortunately as a string. So in my Java Code I have the variable String result which is either "0" or "1". I know (thanks to you guys here) that this string can start with the BOM, so I remove it, if it does.
It is not really necessary but let's say I'd feel better if I had that result as an integer not as a string. The casting from string to int named code seems to work. At least I do not see anything happen.
But when I want to use the casted int like if (code == 1) or display it via Toast, my app crashes.
I can show with Toast that result == "1" and that result.length() == 1 so I do not see how I can possibly fail casting this string to int:
String result = postData("some stuff I send to PHP");
if (result.codePointAt(0) == 65279) // which is the BOM
{result = result.substring(1, result.length());}
int code = Integer.parseInt(result); // <- does not crash here...
// but here...
Toast.makeText(ListView_Confirmation.this, code, Toast.LENGTH_LONG).show();
I also tried using valueOf() and adding .toString() but it just keeps crashing. What am I missing here?
Use the following way to show toast
Toast.makeText(ListView_Confirmation.this, ""+code, Toast.LENGTH_LONG).show();
Toast.makeText requires a String(CharSequence) or an int but this int represents the resource id of the string resource to use (ex: R.string.app_name)
Try instead :
Toast.makeText(ListView_Confirmation.this, String.valueOf(code), Toast.LENGTH_LONG).show();
Use the below
Toast.makeText(ListView_Confirmation.this, String.valueOf(code), Toast.LENGTH_LONG).show();
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#valueOf(int)
public static Toast makeText (Context context, CharSequence text, int duration)
Make a standard toast that just contains a text view.
Parameters
context The context to use. Usually your Application or Activity object.
text The text to show. Can be formatted text.
duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG
http://developer.android.com/reference/android/widget/Toast.html
So use String valueOf(code) as the second parameter to makeText(params)
Returns the string representation of the integer (code) argument.
According to Toast library Toast.makeText(Context, int, int) uses that integer as Resource ID to Find a String in your resources.
Now since you don't have a resource with the same integer the function throws Resources.NotFoundException .
So to display your int value you have to change it back to text.
Toast.makeText(ListView_Confirmation.this, String.valueOf(code), Toast.LENGTH_LONG).show();
In android Api, the constructor of a toast is :
Toast.makeText(Context context, int resId, int duration)
"resId" is the reference of your String but not a String Object:
example: resId= R.string.helloworld
So, searching the internet I found many examples that were not what I need...
Here is my situation:
Imagine that you have a resource that is an array called songs and many 's with each song name..so, imagina you have like 200 song names displayed on a listview..
when you click a song, the app loads another activity with the lyrics for that song...
but HOW do you do that?
Here is how I did it.. I passed the name of the song to this new activity, loaded the resource where the lyrics are...
BUT, at the time I have to setText with the lyrics, I got a problem... should I do a switch with 200 cases? that sounds insane, so, what I did was, I used the same name of the song to the lyrics..so it should load like this:
TextView txtProduct = (TextView) findViewById(R.id.product_label);
Intent i = getIntent();
// getting attached intent data
String[] musicas = getResources().getStringArray(R.array.botafogo_songs);
// this here is where it brings the name of the music to the activity
String product = i.getStringExtra("product");
// displaying selected product name
if (Arrays.asList(musicas).contains(product)) {
//just formating so they have the same name.
String productSplit = product.split("- ")[1];
productSplit.replace(" ", "");
txtProduct.setText(getString(R.string.AbreAlas));
}else{
txtProduct.setText("não contem");
}
Ok, AbreAlas is the name of my resource..it loads fine..
BUT, I cant leave it like this...it should be a variable name...
like: R.string.+productSplit
or something like this..
I can't find the solution nowhere..
You can use getIdentifier():
int resourceName = getResources().getIdentifier(productSplit, "string", getPackageName());
And then:
txtProduct.setText(getString(resourceName));
Don't forget to wrap that in a try and catch.