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());
Related
I'm currently working with Cucumber and Java. I would like to retrieve that path of a file from ITestResult.
I'm currently retrieving the parameters with:
Object[] test = testResult.getParameters();
However the only thing I can access would seem the be the first objects name and nothing else.
test = {Object[1]#1492}
0 = {CucumberFeatureWrapper#1493} "Links at EDM Documents View,"
cucumberFeature = {CucumberFeature#1516}
path = "test/01-automation.feature"
feature = {Feature#1518}
cucumberBackground = null
currentStepContainer = {CucumberScenario#1519}
cucumberTagStatements = {ArrayList#1520} size = 1
i18n = {I18n#1521}
currentScenarioOutline = null
I cannot see anyway of retrieving path = "test/01-automation.feature" under cucumber feature.
Have you tried something like ((CucumberFeatureWrapper)test[0]).getCucumberFeature().getPath()?
I have an imageView that i want to display a little icon of the country that you are currently in. I can get the country code, but problem is i can't dynamically change the imageView resource. My image files are all lowercase
String lowerCountryCode = countryCode.toLowerCase();
String resource = "R.drawable." + lowerCountryCode;
img.setImageResource(resource);
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
setImageResource(id);
abve is my code.
Now, of course this will not work because set Image Resource wants an int, so how can i do this? Thanks in advance
I mean you know the name of files(Drawable) which you wanna use for one and other country.
ImageView imgView=(ImageView)findViewById(R.id.imageView1);
int id = getResources().getIdentifier(lowerCountryCode.toLowerCase(), "drawable", getPackageName());
imgView.setImageDrawable(res.getDrawable(id));
The following code should work well (assuming that you really have a file with name lowerCountryCode in your res/drawable/ folder)
String lowerCountryCode = countryCode.toLowerCase();
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
ivCard[0].setImageResource(id);
i use that code....
ImageView[] ivCard = new ImageView[1];
#override
protected void onCreate(Bundle savedInstanceState)
ivCard[0]=(ImageView)findViewById(R.id.imageView1);
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.
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.