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
}
Related
I am trying to learn retrofit and I have made successful attempts at posting data and now I am trying to retrieve JSON array which looks as follows:
{
"result": "success",
"message": "All Questions Have Been Selected",
"question": {
"all_question_ids": ["1","2","3"]
}
}
I am using the following getter
public ArrayList getAll_question_ids(){
return all_question_ids;
}
I am retrieving using Retrofit as follows
if (resp.getResult().equals(Constants.SUCCESS)) {
SharedPreferences.Editor editor = pref.edit();
Log.d("Question_IDs", "getAllQuestionID() = " + response.body().getQuestion().getAll_question_ids() );
editor.putString(Constants.All_QUESTION_IDS,((resp.getQuestion().getAll_question_ids().toString())));
editor.apply();
}
progress.setVisibility(View.INVISIBLE);
It is here that I am stuck, as I am retrieving the array ok but I am unsure how to loop out the Array which is now stored in Shared Preferences.
When I place a toast to show me how the IDs are coming across, my toast confirms the data as [1,2,3]
The goal is to add a dynamic button and the individual ID, i.e button 1, button 2 etc every-time the loop is iterated.
I have tried the following:
String questionNumber = pref.getString(Constants.All_QUESTION_IDS, "");
for (int i =0; i < questionNumber.length(); i++) {
try {
/*Dynamically create new Button which includes the question name
*/
AppCompatButton btn_question = new AppCompatButton(getActivity());
/*LayoutParams (int width, int height,float weight)
As LayoutParams is defaulted in px, I have called a method called dpToPX to make sure
the dynamically added EditText is the same size on all devices.
*/
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dpToPx(280), dpToPx(45), 1);
btn_question.setBackgroundColor(Color.parseColor("#3B5998"));
btn_question.setTextColor(Color.WHITE);
// btn_question.setText(String.valueOf(x));
btn_question.setText("Question "+ pref.getString(Constants.All_QUESTION_IDS,""));
btn_question.setGravity(Gravity.CENTER);
//generate unique ID for each new EditText dynamically created
View.generateViewId();
//Log.d("TEST VALUE", "Question1 generated ID = " + btn_question.generateViewId());
params.setMargins(0, dpToPx(10), 0, dpToPx(10));
btn_question.setPadding(0, 0, 0, 0);
btn_question.setLayoutParams(params);
allEds.add(btn_question);
mLayout.addView(btn_question);
} catch (Exception e) {
Log.d(TAG, "Failed to create new edit text");
}
}
However the above is adding the value as it appears in the array e.g [1,2,3] which is obviously not what I want.
I have added a photo in case my explanation isn't clear. I want a button with 1 number added to it each time the loop iterates but I am unable to figure this out.
I have looked through lots of resource but cannot find an answer that is relevant to my problem, although, if there is, I am not familiar enough to recognise a similar issue.
If someone can offer some assistance, I would appreciate it!
When you call editor.putString(Constants.All_QUESTION_IDS,((SOMETHING.toString())));, what is actually stored depends on the implementation of the toString method in the type of SOMETHING (in this case String[]). So avoid doing that. Instead, since you're already using Gson or Jackson (or others), store the question_idsas JSON:
final String jsonIds = gson.toJson (resp.getQuestion().getAll_question_ids());
editor.putString(Constants.All_QUESTION_IDS, jsonIds);
Your actual stored value no longer depends on the implementation of something that you don't control (String[].toString). It is a valid JSON array and regardless of what tool/library you use to read it back, it's valid.
Now, to read back the stored data:
final String storedJson = pref.getString(Constants.All_QUESTION_IDS, null);
if (null == storedJson) {
// TODO: No question ids found
}
final String[] ids = gson.fromJson (storedJson, String[].class);
for (int i = 0; i < ids.length; i++) {
// make your buttons
}
This is a problem of saving and then reading out a List of items (in this case, String instances).
You've chosen to save the list by calling editor.putString() with a value of getAll_question_ids().toString(). That toString() call is going to return a string representation of your list, or, in other words, a String instance with the value [1, 2, 3]. At this point, you no longer have a List proper, but a String that looks like a list.
This is all technically fine, but it means you have to take this into account when you're trying to read out that list.
You've written this to read the list back out:
String questionNumber = pref.getString(Constants.All_QUESTION_IDS, "");
Once this line executes, questionNumber will be a String instance with the value [1, 2, 3]. Again, this is fine, but now we come to the key point: we have to convert this String back into a List.
If you know for sure that the values in this list won't have commas in them, you can do it easily:
Trim the braces off the string using substring()
Create a String[] using split()
Convert your array to a list using Arrays.asList() (you could even skip this step since iterating over an array is just as easy as iterating over a list)
Put that together and you get:
String questionNumber = pref.getString(Constants.All_QUESTION_IDS, "");
questionNumber = questionNumber.substring(1, questionNumber.length() - 1);
String[] array = questionNumber.split(", ");
List list = Arrays.asList(array);
At this point, you can iterate over your array or list:
for (String value : list) {
...
btn_question.setText("Question " + value);
...
}
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.
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];
}
I deliver a ArrayList to another method, where I just wanna readout a specific String of the list.
public void pruefeWerHat(ArrayList<Teilnehmer> test){
System.out.println(test);
I get this in LogCat
"1 PeterPan 0 0, 2 Hansi 0 0"
now I just want to use the name, but if I say (after sysout)
String name = test.get(1);
the problem he said to is, that he cannot convert from Teilnehmer to String. I also tested Teilnehmer.get(1) but it doesn't work neither.
When you do
System.out.println(test);
the toString() method is automatically used. This method is in the Object class, so all objects in java can call this method.
When you do
String name = test.get(1);
the toString() method is not called on it's own, you have to call it yourself. To do this, simply use
String name = test.get(1).toString();
Also, if you want to change what is printed, you can overwrite the toString() method in your class.
#Overwrite
public String toString() {
String stringToPrint = "This string will be printed";
return stringToPrint;
}
Now when you do
System.out.println(test);
instead of seeing "1 PeterPan 0 0, 2 Hansi 0 0" you will see "This string will be printed" (or whatever you choose to add in your toString() implementation.
When you print test toString function is called so use this in your code
String name = test.get(1).toString();
What are the members of Teilnehmer?
You need to use something like
string name = (Teilnehmer)test[1].Name
where Name is the field you are trying to extract
The get(int index) method available to ArrayList returns type E. In this instance, it returns type Teilnehmer, which is obviously not a String. You can try and cast Teilnehmer (although probably not desirable) to String or simply call the .toString() method (e.g; test.get(1).toString()) inherited from type Object. Or, if desired, calling a method that returns a String. test.get(1).getNameAsString();
The reason you are allowed to call the type in System.out.println(Teilnehmer) is that println makes a call to the object's string representation:
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
I'm working with Android accelerometer sensor data. I'm going to identify movement patterns of the user by using that sensor data. Most of the work is done. But the problem is tracking continuous axis values and storing them within a 2D array. As a test application I want to fill a double[2][3] array with following kind of data.
[9.594786195244101, 7.118138482942524, 14.240399748761671],
[9.45045389772222, 7.35019779174396, 13.271781719935262]
It is possible to fill the array. But the problem is, I want to re-fill the same array for getting the next set of values. Here is my code for that.
double[][] test;
static int sampleNumber = 1;
static int j = 1;
public void onSensorChanged(SensorEvent event) {
if (started) {
if(sampleNumber == (j*2)-1)
{
test = null;
test = new double[2][3];
j+=1;
}
test[sampleNumber-1][0] = magAvg;
test[sampleNumber-1][1] = magMin;
test[sampleNumber-1][2] = magMax;
sampleNumber += 1;
}
}
When executing this code, a get an ArrayOutOfBoundsException when sampleNumber = 3.
I want to create a new array or start refilling the same array when sampleNumber = 1,3,5,...
How might I do this?
you are creating test array 2*3 anyway...
when sampleNumber = 3 you want to fill array like this:
test[2][0] = magAvg;
test[2][1] = magMin;
test[2][2] = magMax;
But you are creating test array using
new double[2][3]
then test just have 0 and 1 as indexes and accessing index=2 is out of bound exception
you can use a 2-D ArrayList instead of 2-D array in your case.
ArrayList<ArrayList<Integer>>