Can I use a string as an Id? - java

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.

Related

Display multiple elements in card-like disposition with a loop - Android

I'm fairly new with Android development so I'm just trying some concepts.
For this POC I took the first 20 values from this IMDB list => https://www.imdb.com/chart/toptv/
added them into a JSON which is inside the assets folder of my project.
I have a method in my main activity to access the content of the JSON file, it goes like this:
public void readMovies() {
String jsonFileString = Utils.getJsonFromAssets(getApplicationContext(), "movies.json");
Log.i("data", jsonFileString);
Gson gson = new Gson();
Type listUserType = new TypeToken<List<Movies>>() { }.getType();
List<Movies> movies = gson.fromJson(jsonFileString, listUserType);
for (int i = 0; i < movies.size(); i++) {
Log.i("data", "> Item " + i + "\n" + movies.get(i));
}
}
Using a textView I'm able to output all the titles (for example) in the layout, but I'd like to be able to display a view similar to the IMDB page, with an image to the left side, title on the centre and any other property on the right side.
How can I achieve a view like the shared URL?
You've to create a Listview with images and text. Attaching some resources.
Checkout the answers to this Stack overflow question
Check out this Youtube tutorial or this one.
These are the ones I found which provide a code walk-through. But now you know the keywords to search for, you can find resources on your own!

How to fetch a R id in Java using a variable name

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

Add domain to a relative link

I'm fetching some data containing HTML from my server to use in my app, and the html has some links like Go here.
I'm applying the html to a TextView like
text.setText(Html.fromHtml(htmlString));
text.setMovementMethod(LinkMovementMethod.getInstance());
The link works, but causes my app to crash with
No Activity found to handle Intent { act=android.intent.action.VIEW dat=/go/here...`
I'm guessing the crash is because it's a relative link and doesn't have the domain in. Is there any methods, maybe Regex, to search for all <a/> tags and add the domain before it?
I would be tempted to go the simplest way:
final String faultyHtml = "Link: click here etc etc";
final String domain = "http://www.google.fr";
final String fixedHtml = faultyHtml.replace("href=\"/", "href=\"" + domain + "/");
text.setText(Html.fromHtml(fixedHtml));
text.setMovementMethod(LinkMovementMethod.getInstance());
(all occurrences would be added the same domain though)
int index = htmlString.indexOf("<a>");
String part1 = htmlString.subString(0,index+2);
String part2 = htmlString.subString(index+3);
String newHtmlString = part1+"http://"+part2;
You can try like this codes.
TextView tv = (TextView) findViewById(R.id.textView1);
String text = "This is just a test. Click this link here Google to visit google.";
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(Html.fromHtml(text));
reference How to make links in textview clickable in android?

Convert an int-value to a resource uri

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.

Loading pre-set script in with a String

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.

Categories

Resources