Print a String in a specific index in android studio - java

I'm facing a problem printing a String in a specific location on android studio, I tried using charAt() function but the app crashes (for example tv.setText((res.charAt(1))); make it crash)

are you trying to set the text of a test view based off a location inside of a list? thats what it sounds like anyway from the example you gave.
textView.setText(list.get(2);
That will display the test at index 2 in a list.
List<String> list = new ArrayList<>();

If you want to set the text as the character at a specific location in the string, you have to convert the character to a string (using Character.toString()). For example:
tv.setText(Character.toString(res.charAt(1)));

Related

Getting NumberFormatException for TextView.getText()

I'm a beginner who's been desperately trying to understand how to achieve the following thing: I have 3 TextView displaying 3 numeric values (always integers) on screen as text, such as: 50, 100, 200. Then, after some event, I want these 3 strings to be put as values in my PieEntries
in the ArrayList. I can't just get the text from the TextView and put it as a value for the entry, so I tried to use Integer.parseInt to read the Strings as an Ints (since they are actually made of all numbers), or also Integer.valueOf but I only get java.lang.NumberFormatException: For input string: "First" error after the crash. This is the part I'm talking about:
ArrayList<PieEntry> myData(){
ArrayList<PieEntry> myArray = new ArrayList<PieEntry>();
arrayValue.add(new PieEntry(Integer.parseInt(TextView1.getText().toString()), "First"));
arrayValue.add(new PieEntry(Integer.parseInt(TextView2.getText().toString()), "Second"));
arrayValue.add(new PieEntry(Integer.parseInt(TextView3.getText().toString()), "Third"));
return graphValues;
}
I won't paste the whole code here since the graph actually works fine if I manually put values instead of trying to parse the integers from the TextViews.
Also: when I first start to type "new PieEntry", the hint clearly says that it's gonna require a "float value" (then a drawable or a string and so on). Is this where the troubles begin? Should I convert Strings into Integers (or into Floats?) in some way before? I guess there's just like some basic Java rule I'm ignoring, so I'd like to understand what is happening here and how to face this. Many thanks!
As you Explained, you are not getting the TextView text, it is because maybe you have written it somewhere you should not, i.e. provide a Button, when the user types and is done he will hit the button say submit and do this above code on button click.
Solved: I used SharedPreferences to store the new typed data as Integers and retrieve them whenever I need them:
Setting up my Preferences:
SharedPreferences myPreferences;
savedData = getSharedPreferences("myStoredData", Context.MODE_PRIVATE);
Saving an Integer for later:
SharedPreferences.Editor myEditor = savedData.edit();
myEditor.putInt("Label", int);
myEditor.apply();
myEditor.commit();
Retrieving and using the Integer:
savedData.getInt("Label", Default);

How to add more strings in string array with my app in android studio

Hey I'm having a problem with String array, I am using strings arrays and which is working fine now. I want to update some more string in that string array without coding in android studio, like we add photos from our mobile to app, in same manner I want to add more strings to that string array from app, is this possible?
Size of the arrays can not be changed at run time. So once you create an array of any data type, you can't add additional data at run time only you can modify the existing data. If your requirement is to add data at run time, you have to use List or ArrayList.
Ex:
List<String> list = new ArrayList<>();
for(int i = 0; i < 10; i++)
{
list.add("item number " + (i+1));
}
You can any number of data at run time if you use a list.
first of all share some code which you tried but failed, so that I can get an idea what you exactly want to do.
1. you can create an event listener(using button, or anything you want)
2. use list instead of a plain array
3. as soon as event is triggered fetch the string from some text-field and add it to your list

Android: Remove specific part of string that is inside a jsonObject, inside an arraylist

I have a class called GamesData that has strings and getters and setters for this strings.
I download strings from a json. One of these strings is an URL with an image. I download the images but on a small size, because of their URL. I need to download it on a bigger size. For this, I need to remove this "small" string from the URL:
"home_team_logo": "https:\/\/URL\/images\/teams\/small\/olympique-marseille-890.png"
I have more than one URL coming from a big json object, all inside a json array, formated like the one above.
This is what I do to get the json.
arrayList.add(new GamesData(
gamesDataObject.getString(TAG_DATE),
gamesDataObject.getString(TAG_COMPETITION),
gamesDataObject.getString(TAG_HOME_TEAM),
gamesDataObject.getString(TAG_AWAY_TEAM),
gamesDataObject.getString(TAG_ID)
));
I need to remove from TAG_HOME_TEAM
public static final String TAG_HOME_TEAM= "home_team_logo";
, which is that URL above, only the "small" part, so the image downloaded will be the full size one.
In fact I will need this for all 3 tags: TAG_COMPETITION, TAG_HOME_TEAM, TAG_AWAY_TEAM.
How on earth do I do this? xD
You can use replace , use /small/ with / as replacement to avoid matching something like /othersmallteamname/
String s = "https:/URL/images/teams/small/olympique-marseille-890.png";
System.out.println(s.replace("/small/", "/"));
Output :
https:/URL/images/teams/olympique-marseille-890.png
I'm thinking something like this might work:
gamesDataObject.getString(TAG_HOME_TEAM).replaceAll("\/small\/","/");
This uses a regular expression to match all occurrences of the patern. A simplepr less error prone approach may be to use
gamesDataObject.getString(TAG_HOME_TEAM).replace("\/small\/","/");
Which should only replace the first occurrence.

Android Strings issue

My program produces a list of Strings in one activity, then passes it to another activity, and the second activity uses the strings.
When I test by printing out each element of the list at the beginning of the second activity, the printout looks perfect. For example, if I am hoping for the list to contain "Lemon Juice", it prints out exactly right, but yet the logic in the second activity still doesn't work. If I add "Lemon Juice" just like that to the list manually, the logic in the second activity works fine, so the issue is that somehow the string in the received List is not really "Lemon Juice". But:
It prints out exactly correctly (including checking for spaces in front and at the end).
I have tried explicitly casting the received list elements as (String) just to be sure they are Strings.
If I run "Lemon Juice".contains(received String) it comes back true, and if I run received String.contains("Lemon Juice") it
comes back true, but if I run received String.equals("Lemon Juice") it comes back false. This is very confusing to me.
Can anyone think of a possible explanation for how something that is cast as a string, prints as a string, and looks like a string, is not performing like a string?
EDIT to include some code as requested:
// instance variable at top of class--list to which strings will be added for use in
// 2nd activity
private List<String> exs = new ArrayList<String>();
// get array of strings from extra from intent from first activity
String[] recExs = getIntent().getStringArrayExtra(BrowseActivity.EXS);
for (int exx = 0; exx < recExs.length; exx++) {
String curEx = (String) recExs[exx];
exs.add(curEx);
}
Somehow, when I pass exs to the method I need to use the strings, it doesn't work, even though, as explained above, printing and calling contains etc all show that the string, before I added it to exs, was at I wanted it to be.
It's hard to help when you only posted a small snippet of your code.
But I'm guessing the reason String.contains works but String.equals doesn't is that maybe there is space in the Strings. Try String.trim on both side of the Activity when passing and receiving data.

Invoking java methods in XML in android

I am making an app that generates random numbers for android (you can find it hat https://github.com/JXPheonix/RNumGen ) and I need some way for an xml string to be dynamic (of sorts); it needs to change every time it is viewed.
I want the string to invoke a method that generates a random number. The string in my xml file goes something like this:
<string name="number">Your number is</string>
and I want after the is for a method in java to be called upon, which would subsequently change the ending of the string. Any way to do this (whether or not it involves actually editing the strings.xml file?)
You're going about it a round-about way. Instead of somehow making your string in your XML file dynamic, just use your string and append to it in code. Something like:
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(getString(R.string.number) + yourRNGMethod());
Don't change the string, concatenate the constant string with your generated number in your code.
Alternatively, use the string as an input to String.format and use a placeholder for the number.
You don't want to use the strings.xml for strings which have changing values.
Its not clear what the scope for consumption of your random number is but you can always use the Math classes to generate a random number and then Integer.toString() the value to get the value as a string.

Categories