Get all String resources used in activity - android

Is it possible to create a method that would return an array/list of all R.String resources used in an activity?
I'd need something like:
I enter ActivityA
I put to onResume
Log.d(TAG, "Strings used in ActivityA: " + getStringsFromCurrentActivity());
I enter ActivityB and use this method again.
(...)

what is the purpose?
you should define yours Strings in the String.xml file.
Than you should use
String str = getResources().getString(R.string.str_1);
If you want to, you can write a method like:
(but i absolutely see no need to do this)
String [] getStringsForActivity(){
ArrayList<String> al = new ArrayList();
al.put( getResources().getString(R.string.str_1));
al.put( getResources().getString(R.string.xxx));
String[] ar = new String[al.size()];
return al.toArray(ar);
}

Related

Android setter of String array

now i've got simple setter and getter of string array. I want to use setter to put some retrevied json info + same text to array. When i use belowe code:
met.setPlacepic(new String[]{"http://dfsdfsdfsf/" + json.getString("source")});
it looks like setter put only one string to array, despite there is many more data.
Declaration is simple
public String[] placepic
and the setter is also simple:
public void setPlacepic(String[] placepic) {
this.placepic = placepic;
}
Anybody knows reason of this?
If the number of strings is fixed (you know exactly how many element you would have in the array), then you could use String Arrays:
String[] placepic = new String[20]; //20 strings
//Then, in your loop:
placepic[i] = yourData;
If you do NOT know how many strings in your data, You should use List:
List<String> placepicList= new ArrayList<String>();
//Then, in your loop:
placepicList.add(yourData);
//Then after the loop, you get the array
String[] placepic = placepicList.toArray(new String[placepicList.size()]);

How to synchronize global variable Java

String compBut1 = "D0", compBut2 = "D0", compBut3 = "D0", playaBut1 = "D0", playaBut2 = "D0", playaBut3 = "D0";
public void changeOver()
{
String[] set = {playaBut2, playaBut3, playaBut1};
int butPos = Arrays.asList(set).indexOf(positions[posOld]);
set[butPos] = positions[posNew];
}
What must i do to ensure that whenever the value of variables in the array set are changed the global variable also get changed. I can see in the debugger that when I am inside the method the value get changed but as soon as i go out the change is discarded.
String[] set = {playaBut2, playaBut3, playaBut1};
You think the above stores references to the respective strings so that whenever any of these strings is changed, the referenced string changes.
However, what it actually does is copies the values to new instances of String and makes an array of them. Java doesn't allow you to store references (pointers a la C/C++) for safety reasons.
What you should do is: make an array of the globally declared strings and change them directly inside your function.
String[] compBut={"D0","D0","D0"};
String[] playaBut={"D0","D0","D0"};
public void changeOver(){
int butPos = Arrays.asList(playaBut).indexOf(positions[posOld]);
playaBut[butPos] = positions[posNew];
}

Android: Reference R.string.blah based on a method input

I have 3 strings in my strings.xml.
<string name="string1">Hello from string1</string>
<string name="string2">Hello from string2</string>
<string name="string3">Hello from string3</string>
Now, I have a method that I pass a string like "string1" or "string2":
void showStringToast(String sName) {
Toast.makeText(this, getString(R.string.[sName]), Toast.LENGTH_LONG).show();
}
How do I properly reference R.string.sName when sName is being passed as a parameter?
you can pass parameter to your string using like:
<string name="string1">Hello from %1$s (or $d if you want to pass integer) %2$d (second parameter)</string>
Now pass parameter from getstring method:
getString(R.string.string1,first parameter ,second parameter ...);
see this
// try this
void showStringToast(String sName) {
Toast.makeText(this, getString(getResources().getIdentifier(sName, "string", getPackageName())), Toast.LENGTH_LONG).show();
}
I haven't tried this code but I believe you could make use of HashMaps instead.
Declare a static HashMap somewhere, let's say in Constants.java
public static HashMap<String, String> map = new HashMap<String, String>();
then initialize your map. This must be inside a method or constructor
map.put("string1", "Hello from string1");
map.put("string2", "Hello from string2");
map.put("string3", "Hello from string3");
Now you can find your String with something like:
Toast.makeText(this, Constants.map.get(sName).toString(), Toast.LENGTH_LONG).show();
Don't forget to import Constants.java
R.string.something is an integer value that contains a reference to your string. So , R.string.something is like a variable here. Have you ever done such thing that you have generated a variable by appending some string to make it like a variable name and your generated variable name working like a variable ? :)
Instead you can do like this. In your java source file , take your required string values to a String array from your resources. Then use them when necessary. Hope it helps.
Try This way:
void showStringToast(int sName) {
Toast.makeText(this, getString(sName), Toast.LENGTH_LONG).show();
}

Why it doesn't work (string array)?

Hi I wanted to add new string to string array by this method but it doesn't work, because when I start new activity, then favaorites array isn't updated. Why ?
Resources res = getResources();
String[] favorites = res.getStringArray(R.array.favorites);
String[] planets = res.getStringArray(R.array.planets_array);
String[] temp = new String[favorites.length+1];
System.arraycopy(favorites,0,temp,0,favorites.length);
temp[favorites.length] = planets[mCounter];
favorites = temp;
In your case what you can do is use SharedPreferences to store the string into it. No array assigns requires and it is a much cleaner way to do it. Some links to get you started:
http://saigeethamn.blogspot.in/2009/10/shared-preferences-android-developer.html
http://saigeethamn.blogspot.in/2009/10/shared-preferences-android-developer.html
http://developer.android.com/guide/topics/data/data-storage.html
To solve your problem, you should be create an SQLite Database that houses all these properties. Then you should retrieve them from the Cursor (after you query your database) and use the results as necessary
Also another note is that you CANNOT add to the already defined R.array.* because its a precompiled resource.

Android: Using a string in place of an object/instance name

I would like to see if I can avoid a lengthy switch or if block by directly converting some strings into an object name. For example, I have a class called Example and I want to [edit] have up to 10 instances of the class Example1, Example2, so on. Can I use something like:
int ExampleNum = 2;
// can be changed to any 1-10 value corresponding to instances
String s = "Example" + String.valueOf(ExampleNum);
Refresh(s);
public void Refresh(Example example){
...
}
Thus I would create a string with the value of Example2 and pass that to my Refresh method.
[edit]
I don't want to use all the instances at once, but rather have other methods that change the int ExampleNum so that when I try to refresh it refreshes the appropriate Example instance.
Rather than saying:
if (ExampleNum == 2)
Refresh(Example2);
I would use the ExampleNum and String to use the right instance name;
Why not use array's instead??
Example[] e = null;
for(int i=1;i<=10;i++)
{
e[i] = new Example();
Refresh(e[i]);
}
Well, your code, as it stands now, doesn't make any sense since you're passing a String to Refresh, which takes an Example object as an argument.
However, if you're asking how you can create the strings Example1, Example2, ... Example 10, you can do this:
for (int i = 1; i <= 10; i++) {
s = "Example" + i;
refresh(s); // assuming this takes a string
}

Categories

Resources