Ok I have not done a very good job explaining my problem so here goes revised a few times.
I have a Survey, The Survey Produces an Integer Number. I Convert this Number into a String File name Which relates to a Preset String stored in my resources. Based on the choices made on the questions a different string is required at the end.
This code generates the desired command line; R.string.c####
int Q1 = question1.getmCounter();
int Q2 = question2.getmCounter();
int Q3 = question3.getmCounter();
int Q4 = question4.getmCounter();
int qTotal = Q1 + Q2 + Q3 + Q4;
String Test5 = "R.string.c" + qTotal;
And This code is inside onCreate to generate the content for the TextView.
textOut = (TextView) findViewById(R.id.ChmpNametxt);
textOut.setText(Test5);
Now my concept was that it would read Test5 as "R.string.c####" and load the desired string. It does not do this and i would like to know how i can get the contents of Test5 into a commandline.
Hope someon can help me im malting..
Thanks in Advance
-Chris
You got the correct answer here already: Creating Strings than can be used as Filepath - Eclipse / Android
In your case:
String stringId = "c" + qTotal; //note: not the same as what you did with your Test5
int resId = getResources().getIdentifier(stringId, "string", getPackageName());
textOut.setText(resId);
Or are we misunderstandig your use of the word "commandline"?
You need to get the reosurce id for your text, this code gets the resource id for you:
ContextWrapper cw = this.getContext();// how you get this can be different, but you need a ContextWrapper from somewhere to use.
int resId = cw.getResources().getIdentifier("c" + qTotal, "string", cw.getPackageName());
Then you can use textOut.setText with the resId variable as the parameter.
Related
I'm actually making an app in which I have a text area where the user can write in my little "language", nothing hard, the user can write the following lines:
game:
i>5;
i<8;
player:
name=Player 1;
So I concatenate it, lower case it, which gives me the following string: "game:i>5;i<8;player:name=player1;"
Which must give me here two Request objects,
new Request("game", "i>5;i<8")
new Request("player", "name=player1");
Here's an example below
String string = "ygu:tezr;tt;:zertrtrr.etrvz1;tzej:j;ii;,k;i,:kik,;:k:,;ab:";
String part1, part2;
ArrayList<Request> list = new ArrayList<Request>();
/*while(?){
part1 = ?
part2 = ?
list.add(new Request(part1, part2));
}*/
Thanks in advance! :D
If this is the standard format that you mentioned above then you can use following code to get the data -
String s = "game:i>5;i<8;player:name=player1;";
String[] requests = s.split(";");
System.out.println(requests[0].split(":")[0]+" "+requests[0].split(":")[1]+";"+requests[1]);
System.out.println(requests[2].split(":")[0]+" "+requests[2].split(":")[1]);
This is just to give you an idea on how you can break the string.
This what I get as an output when I run the above code -
Output
game i>5;i<8
player name=player1
I'm new to Java and I'm trying to create a poll system using PircBot.
So far my code is this:
if (message.startsWith("!poll")) {
String polly = message.substring(6);
String[] vote = polly.split(" ");
String vote1 = vote[0];
String vote2 = vote[1];
}
Which splits the strings so that someone can type !poll "option1 option2" for example and it will be split into vote1 = option1 and vote2 = option2.
I'm kind of lost from here. Am I even heading in the right direction for creating a voting system?
I figure that I'd have a separate statement as follows.
if (message.equalsIgnoreCase("!vote " + option1))
But I'm not sure where to go with that either.
ImageView test = (ImageView) findViewById(R.id.A1);
gets me an ImageView
instead of A1 I would like to use a string variable to fetch a series of IDs
I have tried
Method Pmethod = Class.getDeclaredMethod("(ImageView) findViewById(Id.R." + dest.toString());
as suggusted in this question : how to call a java method using a variable name?
However I get the error
non-static method getDeclaredMethod cannot be referenced from a static context
Any way to fix this?
My latest attempt :
String ps = "P"+ dest.toString();
String ss = "S"+ dest.toString();
int piecelayertier = getResources().getIdentifier(ps,"id","com.donthaveawebsite.mhy.ocfix");
int selectorlayertier = getResources().getIdentifier(ss,"id","com.donthaveawebsite.mhy.ocfix");
ImageView test =(ImageView)findViewById(piecelayertier);
spot.TieAppearance((ImageView)findViewById(piecelayertier));
spot.TieSelector((ImageView)findViewById(selectorlayertier));
I think this is not a correct way to get id.
You can try following to get id using a string variable :
int id = getResources().getIdentifier("<String id>","id","<package name>");
ImageView test =(ImageView)findViewById(id);
You might also need to use a this reference like so :
int id = this.getResources().getIdentifier("<String id>","id", getPackageName());
i have implemented an android-project which plays a random song. So i have an int-array like this:
int [] playlist_stadt = {R.raw.black_a, R.raw.black_b, R.raw.black_c};
for the random play i wrote:
Random r = new Random();
int i = playlist_stadt[r.nextInt(playlist_stadt.length)];
PlayMusic(i);
what i dont understand is following:
textView.setText(i);
textview shows: res/raw/black_c.mp3
Log.e("Output: ", "" + i);
String uriPath = "android.resource://" + getPackageName() + i;
in the log is i an number and not the same string how in the textview:
Output: 2130968577
203-06 13:09:23.680: E/Output:(31456): android.resource://com.example.testproject2130968577
can s.o. explain me this and how to convert the int-value, that i use it as an resource uri path?
thanks in advance and sry for my english
getResources().getResourceEntryName(i) should get you the mp3 name you are looking for.
i is the resource ID generated by aapt in gen/R.java
Android Accessing Resources Doc
The reason textView.setText(i) returns the mp3 resource name is because you are actually calling setText(int resId)
setText(int resId) JavaDoc
You are passing an int parameter which Android interprets as a resource ID and does the getResourceEntryName conversion for you.
Try using "valueOf(i)", otherwise it will try to look up the location of i and not use the value of i.
Still a Noob at Android Development..
So here's My Code
for(int i=1;i<=6;i++){
for(int j=startat;j<=7;j++){
String constring = "r" + i + "c" + j;
//TextView dtv = (TextView) findViewById(R.id.constring); #commented this out
}
}
Is there a way I could use the string variable constring as an Id?
Is there a way I could use the string variable constring as an Id?
I'm little confused what are trying to make?!
This: TextView dtv = (TextView) findViewById(R.id.constring);
especially R.id.constring you shouldn't, musn't do it in the way you do. Every id is automatic generated from XML resources and you should respect it!
Also R.id.costring is int auto-generated in R.java not String so your approach won't work.
From your code is feel "spaghetti code".
<TextView
android:id=#+id/constring"
... next attributes
/>
If you'll write this, id named as constring will be automatic generated and then you just call R.id so constring is automatic filled.
It a case if you don't want to create XML so you just do it like that:
TextView t = new TextView();
t.setText("Hello World");
setContentView(t);
I recommend to you read some Android tutorials for beginners. Here you can start:
Android - A beginner's
guide
You can use getIdentifier method to obtain resource id from name.