How to synchronize global variable Java - android

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];
}

Related

Convert Array list to Sparse Array

I have copied some code from a project and want to reuse a small part of it in my private app.
The class contains a Sparse Array
public class GolfResult {
String hcpAfter;
String hcpBefore;
SparseArray roundResults;
public GolfResult() {
hcpAfter = "";
hcpBefore = "";
roundResults = new SparseArray();
}
}
I have created an ArrayList for roundResults that is filled with the necessary data.
Then I am trying to fill the instance with content.
GolfResult golferRes = new GolfResult();
SparseArray<RoundResults> hu= new SparseArray<>();
hu = roundresults; // *
golferRes.setHcpAfter("33");
golferRes.setHcpBefore("kk");
golferRes.setRoundResults(hu);
But the problem is that hu = roudresults is not possible, because of the error message:
required: Android.util.SparseArray found: java.util.Array List
Any help will be welcome.
After receiving two helpful answers I got a step further, but now I am facing the problem that my SparseArray hu is empty {}.
The content of hu should be the class roundresults that has the following structure:
public class RoundResults {
boolean actualRound;
private List<HoleResult> holeResults;
Integer roundId;
Integer roundNumber;
String unfinishedReason;
The arrayList roundresults has the size of 1 and has data in the objects.
unfinishedReason =""
holeResults = ArrayLIST size= 18
roundID = "1"
roundNumber = "1"
actualRound = true
hu ={}
mValues = All elements are null
mSize = 0
Does anybody have an idea why?
SparseArray is different than ArrayList, from the documentation:
SparseArrays map integers to Objects. Unlike a normal array of
Objects, there can be gaps in the indices. It is intended to be more
memory efficient than using a HashMap to map Integers to Objects, both
because it avoids auto-boxing keys and its data structure doesn't rely
on an extra entry object for each mapping.
It's using a key value pair principle where the key is an integer and the value which the key mapping is the object. You need to use put [(int key, E value)](https://developer.android.com/reference/android/util/SparseArray.html#put(int, E)) where the E is your object. Remember that:
Adds a mapping from the specified key to the specified value,
replacing the previous mapping from the specified key if there was
one.
So you need to use a loop to add each object in your ArrayList as #valentino-s says:
SparseArray<RoundResults> hu= new SparseArray<>();
for( int i = 0; i < roundresults.size(); i++) {
// i as the key for the object.
hu.put(i, roundresults.get(i));
}
If I understand well your problem, maybe you can try with this:
for ( int i=0; i<roundresults.size(); i++ ) {
hu.put(i,roundresults.get(i));
}
After some trial and error I found a solution for the empty hu:
Instead of put I used append and it is working now.
hu.append(i, roundresults.get(i));
Time for a beer.

Get all String resources used in activity

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);
}

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()]);

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
}

create a new local string variable from value of another variable?

Is is possible to create an new local variable from value of another?
e.g. if value of var1 = "button1" can I construct a new local variable like button1type, ie.using the value of var1 to make part of the new variable
Like this?
String foo = "ohai_" + var1; // Would be "ohai_button1"
If you mean name the variable based on the value in var1? No, but you don't need to.
If you need to associate data based on a string (or other) value, consider using a map.

Categories

Resources