I'm having an arrayList with loads of numbers, I want
to have a different image on every number in the array represented by each number... but I dont know how to put a Variable in (R.drawable."HERE"), if its even possible?!
This is what i've tried to do, Im pretty new to android and Java,
so this may seem pretty funny to you haha... (the array is not shown)
String s = names[position];
String img = s.toString();
holder.imageView.setImageResource(R.drawable.img);
Hope you understand my question.
Thanks in advance
//Halle
You can use reflection do do that. See here for an example.
Related
I write app for Android such gets data from server in JSON format. Now I get this value in string, but in my application it must look like:
Route:
1)first point
2)secon point
3).....
n) n point
I read that in Android in textView I can do it if string will be with html tags but I think it is not the best variant. After Android I must do it in iPhone now I don't know how to do that there. Send Routes as Array is not good variant too. Can you say what is the best way to decide this problem?
Have a look here you will have to find the good pattern .
Hence you have separated strings just use a list View with an ArrayAdapter.
I am not so good with regex but i think it should like : [1-9][0-9]) [[a-f][0-9]]+
I couldn't comment b/c of rep, sorry. Could you provide an example of returned JSON string. I think JSON format can be parsed with ease.
If this the case you can parse it in a loop (or another way. I'm not that good at it)
String[] parseIt (String JSON){
String[] list=JSON.split("\\d\\)");
String[] rlist=new String[list.length-1];
for(int i=0;i<list.length-1;i++){
rlist[i]=list[i+1].trim();
}
return rlist;
}
This might do trick. But you should edit result. I didn't test yet
Edit: I edited code. It simply return the address now with leading whitespace. You can get rid off them using. String trim() method like;
list[1].trim();
Do it in loop and don't care about first element (index 0).
Edit 2: Now it should work
I am having some trouble with my code, and I am trying to figure out where each ImageButton is in my ArrayList to see if the problem is there. How do I retreive the name of an ImageButton from an ArrayList?
List<ImageButton> images = new ArrayList<ImageButton>();
images.add(imgbut1);
images.add(imgbut2);
images.add(imgbut3);
images.add(imgbut4);
images.add(imgbut5);
Here, I want to get imgbut1 for images.get(0), imgbut2 for images.get(1), etc. Something like images.get(0).getName(), images.get(1).getName(), etc
Thanks!
imgbut1, imgbut2 are names you gave to your instances. You cannot get the name.
Here are some good examples
Get name java instances
I am making an android application where i got a set of strings that i load from SharedPreferences so that i can save the strings. The strings contain only numbers, but it is not an int value, its a string value. And i wounder how i can minus the numbers that's in there, becuase usually, i would have been using something like an int value = value - value; But that doesn't seem to work since it's a string and not an int value. How can i do this even though it's a string? I know i could use int values instead, but as i didn't think of this before now, when i'm almost done, it would be alot of work changing all of the code that's related to this. Please help me and thanks so much in advance!
You will have to convert your strings to ints first, then operate on them, then save the string back:
String value = preferences.getString("key:");
int intValue = Integer.valueOf(value);
intValue = intValue - 1;
preferences.edit().putString("key", Integer.toString(intValue)).commit();
Try using Integer.valueOf(string) or Integer.parseInt(string).
Learning basic programming the the concept of casting will help you tremendously. One datatype can be converted to another using the base classes, which often times deal with String. For instance look at the Documentation of Integer.
Well as you have said that it is a lot of work to change the code and save it as int, I would suggest converting the string into an integer, refer to this link for more information, someone has asked about converting strings to integers, and as Android is Java-based, this can apply to your project:
Converting a string to an integer on Android
Hope this helps.
I am using
String [] = {//Variables"}
To create an ArrayList.
I have been made aware the a Vector collection would be alot better.
If anyone know a tutorial, or wants to post an example of a Vector the give specific detail please help me out.
Thanks Alot.
You can use Vector's copyInto(String[]) to build a vector from string array.
Hope this helps.
A Vector is not usually a better choice because it is synchronized and has performance costs.
Also, your code does not create an ArrayList. It is creating an array of Strings which is quite different.
What you really want to do is:
String [] myArray = {/*Variables*};
List<String> myList = Arrays.asList(myArray);
Trying to do something fairly simple.
Taking text like this
User Name: This is a comment I am making
It is in a single TextView. I want to make the User Name a link. I decided that the easiest thing would be to surround the User Name with something like "$#" so it becomes
"$#User Name:$# This is a comment I am making
That way I can use the following regular expression
Pattern userName = Pattern.compile(".*\\$#(.+)\\$#.*");
with Linkify and make it a link. However, clearly I need to remove the delimiters, so the following is the code
title.setText(titleText);
Linkify.TransformFilter transformer = new Linkify.TransformFilter() {
#Override
public String transformUrl(Matcher match, String url) {
return match.group(1);
}
};
Linkify.addLinks(title, userName, "content://user=", null, transformer);
For some reason however, the whole text becomes one giant link, and the text isn't being transformed at all.
It actually did turned out to be pretty easy. I ended up not using the crazy "$#" to delimit the username, instead sticking with just
User Name: This is a comment I am making
so I ended up using the following pattern
Pattern userName = Pattern.compile("(.+:)");
Very simple, and the code becomes just
title.setText(titleText);
Linkify.addLinks(title, GlobalUtil.userName, "user://" + userId + "/");
Thank you to nil for the original suggestion. I was indeed matching the whole string instead of just the userName which is the link.
My best guess is the regex you're using is the problem, where you're telling it to basically pick out the entire string if it matches, including everything before and after what you're looking for. So, the TransformFilter is probably being passed the entire matched string. transformUrl as far as I can tell expects you to return the URL, so the entire string is linked to the first match group.
So, with that in mind, it's probably in your best interest to change the regex to something along the lines of "\\$#(.+?)\\$#" (with an added ? in the group to make the match non-greedy) so as to avoid matching the entire string and just picking out the bit you want to URL-ize (for lack of a better term, plus adding -ize to words sounds cool).
Why not put the delimiters inside the pattern to change ?
Pattern userName = Pattern.compile(".*(\\$#.+\\$#).*");
Then change the transform filter to remove the start and end patterns when changing into the URL...